Class: Filch::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/filch.rb

Overview

similar to Filch::Associations

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Attributes

Returns a new instance of Attributes.



316
317
318
319
# File 'lib/filch.rb', line 316

def initialize(model)
  @model = model
  @ransackable_attributes = @model ? @model.ransackable_attributes : []
end

Instance Method Details

#allObject



341
342
343
344
345
346
347
# File 'lib/filch.rb', line 341

def all
  all_hash = {}
  @ransackable_attributes.each do |attr|
    all_hash[attr] = types[attr_columns(attr)]
  end
  all_hash
end

#attr_columns(attr) ⇒ Object



349
350
351
# File 'lib/filch.rb', line 349

def attr_columns(attr)
  @model.columns.select { |col| col.name == attr }.first.type
end

#attributesObject



330
331
332
333
334
335
336
337
338
339
# File 'lib/filch.rb', line 330

def attributes
  attributes_hash = Hash.new(default)
  if @model.methods.include?(:filch_attributes)
    # attributes_hash.merge(@model.filch_attributes)
    attributes_hash = attributes_hash.merge(@model.filch_attributes)
  end
  # attributes_hash = default_values(attributes_hash)
  attributes_hash.each_value { |template_h| template_h.default = ['eq'] }
  attributes_hash
end

#attributes_keysObject

defaults to Foo.ransackable_attributes



322
323
324
325
326
327
328
# File 'lib/filch.rb', line 322

def attributes_keys
  attributes_keys =
    @model.methods.include?(:filch_attrs) ? @model.filch_attrs : {}
  attributes_keys.default = @ransackable_attributes
  attributes_keys[:all] = @ransackable_attributes
  attributes_keys
end

#defaultObject



353
354
355
356
357
# File 'lib/filch.rb', line 353

def default
  def_hash = Hash.new(['eq'])
  def_hash[:all] = all
  def_hash
end

#typesObject

TODO: setup defaults based on column_type



360
361
362
363
364
365
366
367
# File 'lib/filch.rb', line 360

def types
  {
    integer: %w[eq lt gt],
    string: %w[null eq start matches],
    text: types_hash['string'],
    boolean: %w[null]
  }
end

#types_hashObject



369
370
371
372
373
# File 'lib/filch.rb', line 369

def types_hash
  {
    string: ['eq']
  }
end