Created
July 6, 2017 14:12
-
-
Save sjoulbak/6a33876b12185629fa6024170cba0396 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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