Class: Filch::Associations
- Inherits:
-
Object
- Object
- Filch::Associations
- Defined in:
- lib/filch.rb
Overview
associations_keys, to determine which associations to query & #associations, to determine which predicates to query on those associations.
Instance Method Summary collapse
-
#all ⇒ Object
TODO: specialize defaults to class types using class_eval(assoc.capitalize).columns doesnt work if belongs_to is not the same name as class.
- #all_hash_assoc(all_hash, assoc) ⇒ Object
-
#associations ⇒ Object
a hash representing all the predicates to be queryed for a given association.
-
#associations_keys ⇒ Object
the associated models to be queryed.
-
#initialize(model) ⇒ Associations
constructor
A new instance of Associations.
Constructor Details
#initialize(model) ⇒ Associations
Returns a new instance of Associations.
198 199 200 201 |
# File 'lib/filch.rb', line 198 def initialize(model) @model = model @ransackable_associations = @model ? @model.ransackable_associations : [] end |
Instance Method Details
#all ⇒ Object
TODO: specialize defaults to class types using class_eval(assoc.capitalize).columns doesnt work if belongs_to is not the same name as class
263 264 265 266 267 268 269 270 |
# File 'lib/filch.rb', line 263 def all @ransackable_associations.each_with_object({}) do |assoc, all_hash| all_hash[assoc] = Hash.new(['eq']) next unless Object.const_defined?(assoc.capitalize) all_hash_assoc(all_hash, assoc) # assoc_class_name = assoc.capitalize end end |
#all_hash_assoc(all_hash, assoc) ⇒ Object
272 273 274 275 276 |
# File 'lib/filch.rb', line 272 def all_hash_assoc(all_hash, assoc) Object.const_get(assoc.capitalize).ransackable_attributes .each { |attr| all_hash[assoc][attr] = ['eq'] } all_hash end |
#associations ⇒ Object
a hash representing all the predicates to be queryed for a given association. #associations can be defined in the model
class Foo < ApplicationRecord
has_many :employees
has_many :pay_stubs
def self.filch_associations
{
basic_template: {
employee: {
name: %w[eq cont]
},
pay_stub: {
year: %w[eq gt]
}
}
}
end
end
defaults to all ransackable_attributes for a given association with a value of [‘eq’]
248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/filch.rb', line 248 def associations assoc_hash = if @model.methods.include?(:filch_associations) @model.filch_associations else {} end assoc_hash.default = all assoc_hash[:all] = all assoc_hash end |
#associations_keys ⇒ Object
the associated models to be queryed. associations_keys can be defined in the model
class Foo < ApplicationRecord
has_many :employees
has_many :pay_stubs
def self.filch_assoc_keys
%i[employess pay_stubs]
end
end
defaults to all ransackable_associtations
214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/filch.rb', line 214 def associations_keys assoc_keys = if @model.methods.include?(:filch_assoc_keys) @model.filch_assoc_keys else {} end assoc_keys.default = @ransackable_associations assoc_keys[:all] = @ransackable_associations assoc_keys end |