Class: GroupedScope::Arish::Associations::Builder::GroupedAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/grouped_scope/arish/associations/builder/grouped_association.rb

Direct Known Subclasses

GroupedCollectionAssociation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, ungrouped_name) ⇒ GroupedAssociation

Returns a new instance of GroupedAssociation.



13
14
15
16
17
18
19
# File 'lib/grouped_scope/arish/associations/builder/grouped_association.rb', line 13

def initialize(model, ungrouped_name)
  @model = model
  @ungrouped_name = ungrouped_name
  @ungrouped_reflection = find_ungrouped_reflection
  @grouped_name = :"grouped_scope_#{ungrouped_name}"
  @grouped_options = copy_ungrouped_reflection_options
end

Instance Attribute Details

#grouped_nameObject (readonly)

Returns the value of attribute grouped_name.



7
8
9
# File 'lib/grouped_scope/arish/associations/builder/grouped_association.rb', line 7

def grouped_name
  @grouped_name
end

#grouped_optionsObject (readonly)

Returns the value of attribute grouped_options.



7
8
9
# File 'lib/grouped_scope/arish/associations/builder/grouped_association.rb', line 7

def grouped_options
  @grouped_options
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/grouped_scope/arish/associations/builder/grouped_association.rb', line 7

def model
  @model
end

#ungrouped_nameObject (readonly)

Returns the value of attribute ungrouped_name.



7
8
9
# File 'lib/grouped_scope/arish/associations/builder/grouped_association.rb', line 7

def ungrouped_name
  @ungrouped_name
end

#ungrouped_reflectionObject (readonly)

Returns the value of attribute ungrouped_reflection.



7
8
9
# File 'lib/grouped_scope/arish/associations/builder/grouped_association.rb', line 7

def ungrouped_reflection
  @ungrouped_reflection
end

Class Method Details

.build(model, *association_names) ⇒ Object



9
10
11
# File 'lib/grouped_scope/arish/associations/builder/grouped_association.rb', line 9

def self.build(model, *association_names)
  association_names.each { |ungrouped_name| new(model, ungrouped_name).build }
end

Instance Method Details

#buildObject



21
22
23
24
25
26
27
# File 'lib/grouped_scope/arish/associations/builder/grouped_association.rb', line 21

def build
  model.send(ungrouped_reflection.macro, grouped_name, grouped_options).tap do |grouped_reflection|
    grouped_reflection.grouped_scope = true
    model.grouped_reflections = model.grouped_reflections.merge(ungrouped_name => grouped_reflection)
    define_grouped_scope_reader
  end
end