Class: SmartCollection::Mixin

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

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_config) ⇒ Mixin

Returns a new instance of Mixin.



13
14
15
# File 'lib/smart_collection/mixin.rb', line 13

def initialize raw_config
  @raw_config = raw_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/smart_collection/mixin.rb', line 11

def config
  @config
end

Instance Method Details

#cached_scope(owner) ⇒ Object



36
37
38
# File 'lib/smart_collection/mixin.rb', line 36

def cached_scope owner
  @config.cache_manager.read_scope owner
end

#define_association(base) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/smart_collection/mixin.rb', line 40

def define_association base
  config = @config
  config.cache_manager = CacheManager.new(model: base, config: config)

  mixin_options = {
    name: config.items_name,
    scope: -> owner {
      if owner.new_record?
        uncached_scope(owner)
      else
        cache_manager = config.cache_manager
        unless cache_manager.cache_exists? owner
          owner.update_cache
        end
        cached_scope(owner)
      end
    },
    type: :collection
  }

  cached_name = "cached_#{config.items_name}".to_sym
  mixin_options[:preloader] = -> owners {
    owners.reject(&:cache_exists?).each(&:update_cache)
    Associationist.preload(owners, cached_items: config.item_name)
    owners.map do |owner|
      [owner, owner.cached_items.map{|item| item.send(config.item_name)}]
    end.to_h
  }

  base.include Associationist::Mixin.new(mixin_options)
end

#define_inverse_association(base) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/smart_collection/mixin.rb', line 76

def define_inverse_association base
  return unless @config.inverse_association

  mixin_options = {
    name: @config.inverse_association,
    scope: -> owner {
      expired = base.joins(:cached_items).where(@config.cache_table_name => {item_id: owner.id}).merge(expired_scope base)
      expired.each(&:update_cache)
      base.joins(:cached_items).where(@config.cache_table_name => {item_id: owner.id})
    },
    type: :collection
  }
  @config.item_class.include Associationist::Mixin.new(mixin_options)
end

#expired_scope(base) ⇒ Object



72
73
74
# File 'lib/smart_collection/mixin.rb', line 72

def expired_scope base
  base.where(base.arel_table[:cache_expires_at].lt(Time.now))
end

#included(base) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/smart_collection/mixin.rb', line 91

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

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

  define_association base
  define_inverse_association base
end

#uncached_scope(owner) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/smart_collection/mixin.rb', line 17

def uncached_scope owner
  scopes = @config.scopes_proc.(owner)
  raise unless scopes.all?{|x| x.klass == @config.item_class}
  if scopes.empty?
    @config.item_class.where('1 = 0')
  else
    scopes = scopes.map do |scope|
      if !scope.select_values.empty?
        new_scope = scope.dup
        new_scope.select_values = []
        new_scope
      else
        scope
      end
    end
    @config.item_class.from("(#{scopes.map{|x| "#{x.to_sql}"}.join(' UNION ')}) as #{@config.item_class.table_name}")
  end
end