Class: Adminable::Attributes::Collection
- Inherits:
-
Object
- Object
- Adminable::Attributes::Collection
- Defined in:
- lib/adminable/attributes/collection.rb
Instance Attribute Summary collapse
- #form ⇒ Object
- #index ⇒ Object
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #all ⇒ Object
- #association ⇒ Object
- #belongs_to ⇒ Object
- #columns ⇒ Object
- #has_many ⇒ Object
-
#initialize(model) ⇒ Collection
constructor
A new instance of Collection.
- #ransack ⇒ Object
Constructor Details
#initialize(model) ⇒ Collection
Returns a new instance of Collection.
7 8 9 |
# File 'lib/adminable/attributes/collection.rb', line 7 def initialize(model) @model = model end |
Instance Attribute Details
#form ⇒ Object
15 16 17 |
# File 'lib/adminable/attributes/collection.rb', line 15 def form @form ||= all.except(:id, :created_at, :updated_at) end |
#index ⇒ Object
11 12 13 |
# File 'lib/adminable/attributes/collection.rb', line 11 def index @index ||= all.except(:created_at, :updated_at) end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
4 5 6 |
# File 'lib/adminable/attributes/collection.rb', line 4 def model @model end |
Instance Method Details
#all ⇒ Object
24 25 26 |
# File 'lib/adminable/attributes/collection.rb', line 24 def all [columns, belongs_to, has_many].inject(&:merge) end |
#association ⇒ Object
28 29 30 |
# File 'lib/adminable/attributes/collection.rb', line 28 def association [belongs_to, has_many].inject(&:merge) end |
#belongs_to ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/adminable/attributes/collection.rb', line 44 def belongs_to @belongs_to ||= {}.tap do |attribute| @model.reflect_on_all_associations(:belongs_to).each do |association| attribute[association.name] = Adminable::Attributes::Types::BelongsTo.new( association.name, required: attribute_required?(association.name), association: association ) end end.symbolize_keys end |
#columns ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/adminable/attributes/collection.rb', line 32 def columns @columns ||= {}.tap do |attribute| @model.columns.reject { |a| a.name.match(/_id$/) }.each do |column| attribute[column.name] = "adminable/attributes/types/#{column.type}" .classify.constantize.new( column.name, required: attribute_required?(column.name) ) end end.symbolize_keys end |
#has_many ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/adminable/attributes/collection.rb', line 56 def has_many @has_many ||= {}.tap do |attribute| @model.reflect_on_all_associations(:has_many).each do |association| attribute[association.name] = Adminable::Attributes::Types::HasMany.new( association.name, required: attribute_required?(association.name), association: association ) end end.symbolize_keys end |
#ransack ⇒ Object
19 20 21 22 |
# File 'lib/adminable/attributes/collection.rb', line 19 def ransack @ransack ||= columns.except(:id, :created_at, :updated_at) .select { |k, v| %w(string text).include?(v.type) } end |