Module: PassiveColumns
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/passive_columns.rb,
lib/passive_columns/loader.rb,
lib/passive_columns/railtie.rb,
lib/passive_columns/version.rb,
lib/passive_columns/active_record_relation_extension.rb
Overview
:nodoc:
Defined Under Namespace
Modules: ActiveRecordRelationExtension Classes: Loader, Railtie
Constant Summary collapse
- VERSION =
'0.3.3'
Class Method Summary collapse
-
.apply_select_scope_to(relation) ⇒ void
This method is used to apply the select scope to the relation.
Instance Method Summary collapse
- #_passive_column_loader ⇒ Object
-
#load_column(column) ⇒ any
This method loads a column value, if not already loaded, from the database regardless of whether the column is added to “passive_columns” or not.
Class Method Details
.apply_select_scope_to(relation) ⇒ void
This method returns an undefined value.
This method is used to apply the select scope to the relation. It is used to automatically select all columns except passive columns if no columns are selected.
115 116 117 118 119 120 |
# File 'lib/passive_columns.rb', line 115 def self.apply_select_scope_to(relation) return if relation.klass.try(:_passive_columns).blank? return if relation.select_values.present? relation.select_values = relation.klass.column_names - relation.klass._passive_columns end |
Instance Method Details
#_passive_column_loader ⇒ Object
137 138 139 |
# File 'lib/passive_columns.rb', line 137 def _passive_column_loader @_passive_column_loader ||= PassiveColumns::Loader.new(self, _passive_columns) end |
#load_column(column) ⇒ any
This method loads a column value, if not already loaded, from the database regardless of whether the column is added to “passive_columns” or not.
It uses the Rails’ “.pick” method to get the value of the column under the hood
user = User.select('id').take!
user.load_column(:name) # => SELECT "name" FROM "users" WHERE "id" = ? LIMIT ?
'John'
user.load_column(:name)
'John'
133 134 135 |
# File 'lib/passive_columns.rb', line 133 def load_column(column) _passive_column_loader.load(column, force: true) end |