Class: DeepPluck::Model
- Inherits:
-
Object
- Object
- DeepPluck::Model
- Defined in:
- lib/deep_pluck/model.rb
Defined Under Namespace
Modules: Helper
Instance Method Summary collapse
- #add(args) ⇒ Object
- #delete_extra_column_data! ⇒ Object
- #get_association_scope(reflect) ⇒ Object
- #get_foreign_key(reflect, reverse: false, with_table_name: false) ⇒ Object
- #get_join_table(reflect) ⇒ Object
- #get_primary_key(reflect) ⇒ Object
-
#get_reflect(association_key) ⇒ Object
—————————————————————- ● Reader —————————————————————-.
-
#initialize(relation, parent_association_key = nil, parent_model = nil, need_columns: []) ⇒ Model
constructor
—————————————————————- ● Initialize —————————————————————-.
- #load_all ⇒ Object
- #load_data ⇒ Object
- #use_association_to_query?(reflect) ⇒ Boolean
- #with_conditions(reflect, relation) ⇒ Object
Constructor Details
#initialize(relation, parent_association_key = nil, parent_model = nil, need_columns: []) ⇒ Model
● Initialize
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/deep_pluck/model.rb', line 13 def initialize(relation, parent_association_key = nil, parent_model = nil, need_columns: []) if relation.is_a?(ActiveRecord::Base) @model = relation @relation = nil @klass = @model.class else @model = nil @relation = relation @klass = @relation.klass end @parent_association_key = parent_association_key @parent_model = parent_model @need_columns = need_columns @associations = {} end |
Instance Method Details
#add(args) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/deep_pluck/model.rb', line 100 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
216 217 218 219 220 |
# File 'lib/deep_pluck/model.rb', line 216 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_association_scope(reflect) ⇒ Object
72 73 74 75 |
# File 'lib/deep_pluck/model.rb', line 72 def get_association_scope(reflect) owner = reflect.active_record.new return RailsCompatibility.unscope_where(reflect.association_class.new(owner, reflect).send(:association_scope)) end |
#get_foreign_key(reflect, reverse: false, with_table_name: false) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/deep_pluck/model.rb', line 60 def get_foreign_key(reflect, reverse: false, with_table_name: false) reflect = reflect.chain.last 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
47 48 49 50 51 52 |
# File 'lib/deep_pluck/model.rb', line 47 def get_join_table(reflect) = reflect. return [:through] if [:through] return ([:join_table] || reflect.send(:derive_join_table)) if reflect.macro == :has_and_belongs_to_many return nil end |
#get_primary_key(reflect) ⇒ Object
54 55 56 57 58 |
# File 'lib/deep_pluck/model.rb', line 54 def get_primary_key(reflect) = reflect. return [:primary_key] if [:primary_key] return (reflect.belongs_to? ? reflect.klass : reflect.active_record).primary_key end |
#get_reflect(association_key) ⇒ Object
● Reader
33 34 35 36 37 38 |
# File 'lib/deep_pluck/model.rb', line 33 def get_reflect(association_key) @klass.reflect_on_association(association_key.to_sym) || # add to_sym since rails 3 only support symbol fail(ActiveRecord::ConfigurationError, "ActiveRecord::ConfigurationError: Association named \ '#{association_key}' was not found on #{@klass.name}; perhaps you misspelled it?" ) end |
#load_all ⇒ Object
210 211 212 213 214 |
# File 'lib/deep_pluck/model.rb', line 210 def load_all load_data delete_extra_column_data! return @data end |
#load_data ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/deep_pluck/model.rb', line 195 def load_data columns = get_query_columns key_columns = columns.map(&Helper::TO_KEY_PROC) @relation = yield(@relation) if block_given? @data = loaded_models ? loaded_models.as_json(root: false, only: key_columns) : pluck_values(columns) if @data.size != 0 # for delete_extra_column_data! @extra_columns = key_columns - @need_columns.map(&Helper::TO_KEY_PROC) @associations.each do |key, model| set_includes_data(@data, key, model) end end return @data end |
#use_association_to_query?(reflect) ⇒ Boolean
77 78 79 |
# File 'lib/deep_pluck/model.rb', line 77 def use_association_to_query?(reflect) reflect.through_reflection && reflect.chain.first.macro == :has_one end |
#with_conditions(reflect, relation) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/deep_pluck/model.rb', line 40 def with_conditions(reflect, relation) = reflect. relation = relation.instance_exec(&reflect.scope) if reflect.respond_to?(:scope) and reflect.scope relation = relation.where([:conditions]) if [:conditions] return relation end |