Class: SmartCollection::Mixin

Inherits:
Module
  • Object
show all
Defined in:
lib/smart_collection/mixin.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_config) ⇒ Mixin

Returns a new instance of Mixin.



31
32
33
# File 'lib/smart_collection/mixin.rb', line 31

def initialize raw_config
  @raw_config = raw_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



29
30
31
# File 'lib/smart_collection/mixin.rb', line 29

def config
  @config
end

Instance Method Details

#included(base) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/smart_collection/mixin.rb', line 35

def included base
  @config = config = SmartCollection::Config.new(@raw_config)

  reflection_options = {smart_collection: config}
  if config.item_class_name
    reflection_options[:class_name] = config.item_class_name
  end

  reflection = Builder::SmartCollectionAssociation.build(base, config.items_name, nil, reflection_options)
  ::ActiveRecord::Reflection.add_reflection base, config.items_name, reflection

  base.include(InstanceMethods)
  base.extend(ClassMethods)

  if cache_class = CacheManager.determine_class(@raw_config)
    config.cache_manager = cache_class.new(model: base, config: config)
  end

end