Skip to content

Instantly share code, notes, and snippets.

@sjoulbak
Created July 6, 2017 14:12
Show Gist options
  • Save sjoulbak/6a33876b12185629fa6024170cba0396 to your computer and use it in GitHub Desktop.
Save sjoulbak/6a33876b12185629fa6024170cba0396 to your computer and use it in GitHub Desktop.
module SeoMeta
module ExtraInstanceMethods
def attributes
seo_meta_attributes.merge(super)
end
def attributes_equals(attributes, *args)
seo_meta_attributes.merge(attributes)
super
end
end
end
module SeoMeta
module InstanceMethods
class << self
prepend ::SeoMeta::ExtraInstanceMethods
def included(base)
# This has to be introduced using module_eval because it overrides something.
base.module_eval do
def seo_meta
find_seo_meta_tags || build_seo_meta_tags
end
alias_method :attributes_equals, :attributes=
end
end
end
def seo_meta_attributes
::SeoMeta.attributes.keys.inject({}) { |attrs, name|
attrs.merge(name.to_s => send(name))
}
end
protected
def find_seo_meta_tags
@seo_meta ||= ::SeoMetum.where(:seo_meta_type => self.class.name,
:seo_meta_id => self.id).first
end
def build_seo_meta_tags
@seo_meta ||= ::SeoMetum.new :seo_meta_type => self.class.name
end
def save_meta_tags!
seo_meta.seo_meta_id ||= self.id unless seo_meta.persisted?
seo_meta.save
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment