Sha256: f6b4893a91d996c14c29d0f1b4f128c4d3065ace39c203b240df88c41c950f88

Contents?: true

Size: 1.31 KB

Versions: 5

Compression:

Stored size: 1.31 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Document
    module InstanceMethods
      def parentize_with_custom_fields(object, association_name)
        parentize_without_custom_fields(object, association_name)
        
        if self.custom_fields?(object, association_name)          
          # puts "[parentize_with_custom_fields] association_name = #{association_name} / #{self.custom_fields_association_name(association_name)}"          
          object.send(self.custom_fields_association_name(association_name)).each do |field|
            field.apply(self, association_name)
          end
          
          self.instance_eval <<-EOV
            def custom_fields
              fields = self._parent.send(:#{self.custom_fields_association_name(association_name)})
              fields.sort { |a, b| (a.position || 0) <=> (b.position || 0) }
            end
          EOV
        end
      end
      
      alias_method_chain :parentize, :custom_fields
      
      def custom_fields_association_name(association_name)
        "#{association_name.to_s.singularize}_custom_fields".to_sym
      end
      
      def custom_fields?(object, association_name)
        object.respond_to?(custom_fields_association_name(association_name)) &&
        object.associations[association_name]
      end      
    end    
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
locomotive_cms-0.0.1.4 vendor/plugins/custom_fields/lib/custom_fields/extensions/mongoid/document.rb
locomotive_cms-0.0.1.3 vendor/plugins/custom_fields/lib/custom_fields/extensions/mongoid/document.rb
locomotive_cms-0.0.1.2 vendor/plugins/custom_fields/lib/custom_fields/extensions/mongoid/document.rb
locomotive_cms-0.0.1.1 vendor/plugins/custom_fields/lib/custom_fields/extensions/mongoid/document.rb
locomotive_cms-0.0.1 vendor/plugins/custom_fields/lib/custom_fields/extensions/mongoid/document.rb