Class: DeepPluck::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/deep_pluck/model.rb

Defined Under Namespace

Modules: Helper

Instance Method Summary collapse

Constructor Details

#initialize(relation, parent_association_key = nil, parent_model = nil) ⇒ Model


Initialize



7
8
9
10
11
12
13
# File 'lib/deep_pluck/model.rb', line 7

def initialize(relation, parent_association_key = nil, parent_model = nil)
  @relation = relation
  @parent_association_key = parent_association_key
  @parent_model = parent_model
  @need_columns = []
  @associations = {}
end

Instance Method Details

#add(args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/deep_pluck/model.rb', line 59

def add(args)
  return self if args == nil
  args = [args] if not args.is_a?(Array)
  args.each do |arg|
    case arg
    when Hash ; add_association(arg)
    else      ; add_need_column(arg)
    end
  end
  return self
end

#delete_extra_column_data!Object



116
117
118
119
120
# File 'lib/deep_pluck/model.rb', line 116

def delete_extra_column_data!
  return if @data.blank?
  @data.each{|s| s.except!(*@extra_columns) }
  @associations.each{|_, model| model.delete_extra_column_data! }
end

#get_foreign_key(reflect, reverse: false, with_table_name: false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/deep_pluck/model.rb', line 35

def get_foreign_key(reflect, reverse: false, with_table_name: false)
  if reverse and (table_name = get_join_table(reflect)) #reverse = parent
    key = reflect.chain.last.foreign_key
  else
    key = (reflect.belongs_to? == reverse ? get_primary_key(reflect) : reflect.foreign_key)
    table_name = (reverse ? reflect.klass : reflect.active_record).table_name
  end
  return "#{table_name}.#{key}" if with_table_name
  return key.to_s #key may be symbol if specify foreign_key in association options
end

#get_join_table(reflect) ⇒ Object



27
28
29
30
31
# File 'lib/deep_pluck/model.rb', line 27

def get_join_table(reflect)
  return reflect.options[:through] if reflect.options[:through]
  return (reflect.options[:join_table] || reflect.send(:derive_join_table)) if reflect.macro == :has_and_belongs_to_many
  return nil
end

#get_primary_key(reflect) ⇒ Object



32
33
34
# File 'lib/deep_pluck/model.rb', line 32

def get_primary_key(reflect)
  return (reflect.belongs_to? ? reflect.klass : reflect.active_record).primary_key
end

#get_reflect(association_key) ⇒ Object


Reader



17
18
19
20
# File 'lib/deep_pluck/model.rb', line 17

def get_reflect(association_key)
  @relation.klass.reflect_on_association(association_key.to_sym) || #add to_sym since rails 3 only support symbol
    raise(ActiveRecord::ConfigurationError, "ActiveRecord::ConfigurationError: Association named '#{association_key}' was not found on #{@relation.klass.name}; perhaps you misspelled it?")
end

#load_allObject



111
112
113
114
115
# File 'lib/deep_pluck/model.rb', line 111

def load_all
  load_data
  delete_extra_column_data!
  return @data
end

#load_dataObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/deep_pluck/model.rb', line 96

def load_data
  prev_need_columns = @parent_model.get_foreign_key(@parent_model.get_reflect(@parent_association_key), reverse: true, with_table_name: true) if @parent_model
  next_need_columns = @associations.map{|key, _| get_foreign_key(get_reflect(key), with_table_name: true) }.uniq
  all_need_columns = [*prev_need_columns, *next_need_columns, *@need_columns].uniq(&Helper::TO_KEY_PROC)
  @relation = yield(@relation) if block_given?
  @data = @relation.pluck_all(*all_need_columns)
  if @data.size != 0
    #for delete_extra_column_data!
    @extra_columns = all_need_columns.map(&Helper::TO_KEY_PROC) - @need_columns.map(&Helper::TO_KEY_PROC)
    @associations.each do |key, model|
      set_includes_data(@data, key, model)
    end
  end
  return @data
end

#with_conditions(reflect, relation) ⇒ Object



21
22
23
24
25
26
# File 'lib/deep_pluck/model.rb', line 21

def with_conditions(reflect, relation)
  options = reflect.options
  relation = relation.instance_exec(&reflect.scope) if reflect.respond_to?(:scope) and reflect.scope
  relation = relation.where(options[:conditions]) if options[:conditions]
  return relation
end