Class: ActiveRecord::Relation
- Inherits:
-
Object
- Object
- ActiveRecord::Relation
- Defined in:
- lib/pluck_all/models/active_record_extension.rb,
lib/pluck_all/models/active_record_extension.rb
Instance Method Summary collapse
-
#cast_carrier_wave_uploader_url(attributes) ⇒ Object
—————————————————————- ● Support casting CarrierWave url —————————————————————-.
- #cast_need_columns(column_names, _klass = nil) ⇒ Object
- #pluck_all(*column_names, cast_uploader_url: true) ⇒ Object
- #select_all(column_names) ⇒ Object
Instance Method Details
#cast_carrier_wave_uploader_url(attributes) ⇒ Object
● Support casting CarrierWave url
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/pluck_all/models/active_record_extension.rb', line 79 def cast_carrier_wave_uploader_url(attributes) if defined?(CarrierWave) && klass.respond_to?(:uploaders) @pluck_all_cast_need_columns ||= nil @pluck_all_cast_klass ||= klass @pluck_all_uploaders ||= @pluck_all_cast_klass.uploaders.select{|key, _uploader| attributes.key?(key.to_s) } @pluck_all_uploaders.each do |key, _uploader| {}.tap do |hash| @pluck_all_cast_need_columns.each{|k| hash[k] = attributes[k] } if @pluck_all_cast_need_columns obj = @pluck_all_cast_klass.instantiate(hash) obj[key] = attributes[key_s = key.to_s] # https://github.com/carrierwaveuploader/carrierwave/blob/87c37b706c560de6d01816f9ebaa15ce1c51ed58/lib/carrierwave/mount.rb#L142 attributes[key_s] = obj.send(key) end end end return attributes end |
#cast_need_columns(column_names, _klass = nil) ⇒ Object
6 7 8 9 10 |
# File 'lib/pluck_all/models/active_record_extension.rb', line 6 def cast_need_columns(column_names, _klass = nil) @pluck_all_cast_need_columns = column_names.map(&:to_s) @pluck_all_cast_klass = _klass return self end |
#pluck_all(*column_names, cast_uploader_url: true) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/pluck_all/models/active_record_extension.rb', line 28 def pluck_all(*column_names, cast_uploader_url: true) result = select_all(column_names) casted_result = RailsCompatibility.cast_values(klass, result) casted_result.each{|attributes| cast_carrier_wave_uploader_url(attributes) } if cast_uploader_url return casted_result end |
#select_all(column_names) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/pluck_all/models/active_record_extension.rb', line 12 def select_all(column_names) relation = clone # See: https://github.com/globalize/globalize/pull/707 if relation.klass.method_defined?(:translated_attribute_names) && (parsed = parse_translated_columns(column_names)) relation = relation.join_translations column_names = parsed end relation.select_values = [].freeze # cannot use `unscope(:select)` in Rails 3 sql = relation.select(column_names.map(&to_sql_column_name)).to_sql return klass.connection.select_all(sql) end |