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



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

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



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/deep_pluck/model.rb', line 47

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



125
126
127
128
129
# File 'lib/deep_pluck/model.rb', line 125

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



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/deep_pluck/model.rb', line 20

def get_foreign_key(reflect, reverse: false, with_table_name: false)
  if reflect.options[:through] and reverse #reverse = parent
    chain_reflect = reflect.chain.last
    table_name = chain_reflect.table_name
    key = chain_reflect.foreign_key
  else
    return (reflect.belongs_to? ? reflect.active_record.primary_key : reflect.foreign_key) if reverse
    table_name = reflect.active_record.table_name
    key = (reflect.belongs_to? ? reflect.foreign_key : reflect.active_record.primary_key)
  end
  return key if !with_table_name
  return "#{table_name}.#{key}"
end

#get_reflect(association_key) ⇒ Object


Reader



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

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



120
121
122
123
124
# File 'lib/deep_pluck/model.rb', line 120

def load_all
  load_data
  delete_extra_column_data!
  return @data
end

#load_dataObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/deep_pluck/model.rb', line 105

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