Sha256: 62542afb17f937a1bb274ea55cf2ad26e2fe3df33f14f627d24c7139a62e5d37

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 KB

Contents

module MetaManager
  module Taggable
    extend ::ActiveSupport::Concern
    
    included do          
      has_many :meta_tags, :as => :taggable, :dependent => :destroy, :autosave => true
        
      accepts_nested_attributes_for :meta_tags, :reject_if => :all_blank, :allow_destroy => true
    end
    
    # Find meta tag by name or build new record
    def find_or_build_meta_tag(attr_name)
      key = normalize_meta_tag_name(attr_name)
      self.meta_tags.where(:name => key).first || self.meta_tags.build(:name => key)
    end
    
    # Save meta tags records into one hash
    def meta_tag(attr_name)
      key = normalize_meta_tag_name(attr_name)
      @meta_tag ||= {}
      @meta_tag[key] ||= find_or_build_meta_tag(key)
    end
    
    def respond_to?(method_sym, include_private = false)
      if method_sym.to_s =~ meta_match_case
        true
      else
        super
      end
    end
    
    protected
    
    def normalize_meta_tag_name(value)
      value.to_s.downcase.strip
    end
    
    def method_missing(method, *args, &block)
      key = method.to_s
      
      if key =~ meta_match_case
        attr_name = key.gsub(meta_match_case, '')
        
        if key =~ /=$/
          record = meta_tag(attr_name.chop)
          record.content = args.first
        else 
          meta_tag(attr_name).try(:content)
        end
      else
        super
      end
    end
    
    def meta_match_case
      /^tag_/
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
sunrise-cms-0.3.3 vendor/bundle/ruby/1.9.1/gems/meta_manager-0.0.6/lib/meta_manager/taggable.rb
sunrise-cms-0.3.2 vendor/bundle/ruby/1.9.1/gems/meta_manager-0.0.6/lib/meta_manager/taggable.rb
sunrise-cms-0.3.1 vendor/bundle/ruby/1.9.1/gems/meta_manager-0.0.6/lib/meta_manager/taggable.rb
sunrise-cms-0.3.0 vendor/bundle/ruby/1.9.1/gems/meta_manager-0.0.6/lib/meta_manager/taggable.rb
meta_manager-0.0.6 lib/meta_manager/taggable.rb