Sha256: cdf598b8e7567bfd8dbe7d4b7d3d29421dc9b95a4088a863fc9072c57691b486

Contents?: true

Size: 1019 Bytes

Versions: 5

Compression:

Stored size: 1019 Bytes

Contents

module GroupedScope
  module ClassMethods
    
    def grouped_scopes
      read_inheritable_attribute(:grouped_scopes) || write_inheritable_attribute(:grouped_scopes, {})
    end
    
    def grouped_scope(*associations)
      create_belongs_to_for_grouped_scope
      associations.each { |association| AssociationReflection.new(self,association) }
      create_grouped_scope_accessor
    end
    
    private
    
    def create_grouped_scope_accessor
      define_method(:group) do
        @group ||= SelfGroupping.new(self)
      end
    end
    
    def create_belongs_to_for_grouped_scope
      grouping_class_name = 'GroupedScope::Grouping'
      existing_grouping = reflections[:grouping]
      return false if existing_grouping && existing_grouping.macro == :belongs_to && existing_grouping.options[:class_name] == grouping_class_name
      belongs_to :grouping, :foreign_key => 'group_id', :class_name => grouping_class_name
    end
    
  end
end

ActiveRecord::Base.send :extend, GroupedScope::ClassMethods

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
decisiv-grouped_scope-0.5.1.1 lib/grouped_scope/class_methods.rb
decisiv-grouped_scope-0.6.0.1 lib/grouped_scope/class_methods.rb
metaskills-grouped_scope-0.5.1 lib/grouped_scope/class_methods.rb
metaskills-grouped_scope-0.6.0 lib/grouped_scope/class_methods.rb
grouped_scope-0.6.1 lib/grouped_scope/class_methods.rb