Class: ActiveScaffold::DataStructures::Columns
- Includes:
- Configurable, Enumerable
- Defined in:
- lib/active_scaffold/data_structures/columns.rb
Instance Attribute Summary collapse
-
#active_record_class ⇒ Object
readonly
This accessor is used by ActionColumns to create new Column objects without adding them to this set.
Instance Method Summary collapse
- #_inheritable ⇒ Object
-
#_inheritable=(value) ⇒ Object
The motivation for this collection is that this Columns data structure fills two roles: it provides the master list of all known columns, and it provides an inheritable list for all other actions (e.g. Create and Update and List).
-
#add(*args) ⇒ Object
(also: #<<)
the way to add columns to the set.
-
#add_association_columns(association, *columns) ⇒ Object
add columns from association (belongs_to or has_one) these columns will use label translation from association model they will be excluded, so won’t be included in action columns association columns will work for read actions only, not in form actions (create, update, subform).
- #each ⇒ Object
- #exclude(*args) ⇒ Object
-
#find_by_name(name) ⇒ Object
(also: #[])
returns the column of the given name.
-
#find_by_names(*names) ⇒ Object
returns an array of columns with the provided names.
-
#initialize(active_record_class, *args) ⇒ Columns
constructor
A new instance of Columns.
Methods included from Configurable
Constructor Details
#initialize(active_record_class, *args) ⇒ Columns
Returns a new instance of Columns.
22 23 24 25 26 27 28 |
# File 'lib/active_scaffold/data_structures/columns.rb', line 22 def initialize(active_record_class, *args) @active_record_class = active_record_class @_inheritable = [] @set = [] add(*args) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ActiveScaffold::Configurable
Instance Attribute Details
#active_record_class ⇒ Object (readonly)
This accessor is used by ActionColumns to create new Column objects without adding them to this set
20 21 22 |
# File 'lib/active_scaffold/data_structures/columns.rb', line 20 def active_record_class @active_record_class end |
Instance Method Details
#_inheritable ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/active_scaffold/data_structures/columns.rb', line 83 def _inheritable if @sorted @_inheritable else @_inheritable.sort do |a, b| self[a] <=> self[b] end end end |
#_inheritable=(value) ⇒ Object
The motivation for this collection is that this Columns data structure fills two roles: it provides the master list of all known columns, and it provides an inheritable list for all other actions (e.g. Create and Update and List). Well we actually want to know about as many columns as possible, so we don’t want people actually removing columns from @set. But at the same time, we want to be able to manage which columns get inherited. Tada!
This collection is referenced by other parts of ActiveScaffold and by methods within this DataStructure. IT IS NOT MEANT FOR PUBLIC USE (but if you know what you’re doing, go ahead)
14 15 16 17 |
# File 'lib/active_scaffold/data_structures/columns.rb', line 14 def _inheritable=(value) @sorted = true @_inheritable = value end |
#add(*args) ⇒ Object Also known as: <<
the way to add columns to the set. this is primarily useful for virtual columns. note that this also makes columns inheritable
32 33 34 35 36 37 38 39 40 |
# File 'lib/active_scaffold/data_structures/columns.rb', line 32 def add(*args) args.flatten! # allow [] as a param args = args.collect(&:to_sym) # make the columns inheritable @_inheritable.concat(args) # then add columns to @set (unless they already exist) args.each { |a| @set << ActiveScaffold::DataStructures::Column.new(a.to_sym, @active_record_class) unless find_by_name(a) } end |
#add_association_columns(association, *columns) ⇒ Object
add columns from association (belongs_to or has_one) these columns will use label translation from association model they will be excluded, so won’t be included in action columns association columns will work for read actions only, not in form actions (create, update, subform)
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/active_scaffold/data_structures/columns.rb', line 47 def add_association_columns(association, *columns) column = self[association] raise ArgumentError, "unknown column #{association}" if column.nil? raise ArgumentError, "column #{association} is not an association" if column.association.nil? raise ArugmentError, "column #{association} is not singular association" unless column.association.singular? raise ArugmentError, "column #{association} is polymorphic association" if column.association.polymorphic? klass = column.association.klass columns.each do |col| next if find_by_name col @set << ActiveScaffold::DataStructures::Column.new(col, klass, column.association) end end |
#each ⇒ Object
79 80 81 |
# File 'lib/active_scaffold/data_structures/columns.rb', line 79 def each @set.each { |i| yield i } end |
#exclude(*args) ⇒ Object
61 62 63 64 |
# File 'lib/active_scaffold/data_structures/columns.rb', line 61 def exclude(*args) # only remove columns from _inheritable. we never want to completely forget about a column. args.each { |a| @_inheritable.delete a.to_sym } end |
#find_by_name(name) ⇒ Object Also known as: []
returns the column of the given name.
72 73 74 75 76 |
# File 'lib/active_scaffold/data_structures/columns.rb', line 72 def find_by_name(name) # this works because of `def column.==' column = @set.find { |c| c == name } column end |
#find_by_names(*names) ⇒ Object
returns an array of columns with the provided names
67 68 69 |
# File 'lib/active_scaffold/data_structures/columns.rb', line 67 def find_by_names(*names) @set.find_all { |column| names.include? column.name } end |