Class: Adminable::Attributes::Collection
- Inherits:
-
Object
- Object
- Adminable::Attributes::Collection
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/adminable/attributes/collection.rb
Instance Attribute Summary collapse
-
#all ⇒ Object
readonly
Returns the value of attribute all.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
-
#add(name, type, options = {}) ⇒ Object
Adds new attribute to collection.
-
#associations ⇒ Array
Collects attributes from model associations.
-
#columns ⇒ Array
Collects attributes from model columns rubocop:disable Metrics/MethodLength.
- #configure ⇒ Object
- #form ⇒ Object
-
#get(name) ⇒ Object
Finds attribute by name.
- #index ⇒ Object
-
#initialize(model) ⇒ Collection
constructor
A new instance of Collection.
- #search ⇒ Object
-
#set(*args) ⇒ Object
Changes options for given attribute.
Constructor Details
#initialize(model) ⇒ Collection
12 13 14 15 |
# File 'lib/adminable/attributes/collection.rb', line 12 def initialize(model) @model = model @all ||= columns + associations end |
Instance Attribute Details
#all ⇒ Object (readonly)
Returns the value of attribute all.
9 10 11 |
# File 'lib/adminable/attributes/collection.rb', line 9 def all @all end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
9 10 11 |
# File 'lib/adminable/attributes/collection.rb', line 9 def model @model end |
Instance Method Details
#add(name, type, options = {}) ⇒ Object
Adds new attribute to collection
94 95 96 |
# File 'lib/adminable/attributes/collection.rb', line 94 def add(name, type, = {}) @all << resolve(type).new(name, **) end |
#associations ⇒ Array
Collects attributes from model associations
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/adminable/attributes/collection.rb', line 50 def associations @associations ||= [].tap do |attributes| @model.reflect_on_all_associations.each do |association| attributes << resolve(association.macro).new( association.name, required: required?(association.name), association: association ) end end end |
#columns ⇒ Array
Collects attributes from model columns rubocop:disable Metrics/MethodLength
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/adminable/attributes/collection.rb', line 33 def columns @columns ||= [].tap do |attributes| @model.columns.reject { |a| a.name.match(/_id$/) }.each do |column| begin attributes << resolve(column.type).new( column.name, required: required?(column.name) ) rescue Adminable::AttributeNotImplemented next end end end end |
#configure ⇒ Object
62 63 64 |
# File 'lib/adminable/attributes/collection.rb', line 62 def configure yield end |
#form ⇒ Object
21 22 23 |
# File 'lib/adminable/attributes/collection.rb', line 21 def form all.select { |attribute| attribute.[:form] } end |
#get(name) ⇒ Object
Finds attribute by name
83 84 85 86 87 88 89 |
# File 'lib/adminable/attributes/collection.rb', line 83 def get(name) @all.find { |attribute| attribute.name == name } || raise( Adminable::AttributeNotFound, "couldn't find attribute with name `#{name}`" ) end |
#index ⇒ Object
17 18 19 |
# File 'lib/adminable/attributes/collection.rb', line 17 def index all.select { |attribute| attribute.[:index] } end |
#search ⇒ Object
25 26 27 |
# File 'lib/adminable/attributes/collection.rb', line 25 def search all.select { |attribute| attribute.[:search] } end |
#set(*args) ⇒ Object
Changes options for given attribute
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/adminable/attributes/collection.rb', line 69 def set(*args) = args. names = args .each do |key, value| names.each do |name| get(name).[key] = value end end end |