Class: ActiveList::Definition::Table
- Inherits:
-
Object
- Object
- ActiveList::Definition::Table
- Defined in:
- lib/active_list/definition/table.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
Instance Method Summary collapse
-
#action(name, options = {}) ⇒ Object
Add a new method in Table which permit to define action columns.
- #action_columns ⇒ Object
-
#check_box(name, options = {}) ⇒ Object
Add a new method in Table which permit to define check_box columns.
- #children ⇒ Object
-
#column(name, options = {}) ⇒ Object
Add a new method in Table which permit to define data columns.
- #data_columns ⇒ Object
-
#empty(name, options = {}) ⇒ Object
Add a new method in Table which permit to define empty columns.
- #exportable_columns ⇒ Object
- #global_action_columns ⇒ Object
- #has_many_records_action? ⇒ Boolean
- #hidden_columns ⇒ Object
-
#initialize(name, model = nil, options = {}) ⇒ Table
constructor
A new instance of Table.
- #load_default_columns ⇒ Object
- #many_records_action_columns ⇒ Object
- #model_columns ⇒ Object
- #new_column_id ⇒ Object
- #paginate? ⇒ Boolean
-
#reflections ⇒ Object
Compute includes Hash.
- #selectable? ⇒ Boolean
- #sortable_columns ⇒ Object
- #status(*args) ⇒ Object
-
#table_columns ⇒ Object
Retrieves all columns in database.
-
#text_field(name, options = {}) ⇒ Object
Add a new method in Table which permit to define text_field columns.
Constructor Details
#initialize(name, model = nil, options = {}) ⇒ Table
Returns a new instance of Table.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/active_list/definition/table.rb', line 6 def initialize(name, model = nil, = {}) @name = name @model = model || name.to_s.classify.constantize = @paginate = !([:pagination] == :none || [:paginate].is_a?(FalseClass)) [:renderer] ||= :simple_renderer [:per_page] = 20 if [:per_page].to_i <= 0 [:page] = 1 if [:page].to_i <= 0 @columns = [] @id = ActiveList.new_uid end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
4 5 6 |
# File 'lib/active_list/definition/table.rb', line 4 def columns @columns end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
4 5 6 |
# File 'lib/active_list/definition/table.rb', line 4 def id @id end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
4 5 6 |
# File 'lib/active_list/definition/table.rb', line 4 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/active_list/definition/table.rb', line 4 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/active_list/definition/table.rb', line 4 def end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
4 5 6 |
# File 'lib/active_list/definition/table.rb', line 4 def parameters @parameters end |
Instance Method Details
#action(name, options = {}) ⇒ Object
Add a new method in Table which permit to define action columns
102 103 104 |
# File 'lib/active_list/definition/table.rb', line 102 def action(name, = {}) add :action, name, end |
#action_columns ⇒ Object
71 72 73 |
# File 'lib/active_list/definition/table.rb', line 71 def action_columns @columns.select { |c| c.is_a? ActionColumn } end |
#check_box(name, options = {}) ⇒ Object
Add a new method in Table which permit to define check_box columns
97 98 99 |
# File 'lib/active_list/definition/table.rb', line 97 def check_box(name, = {}) add :check_box, name, end |
#children ⇒ Object
37 38 39 |
# File 'lib/active_list/definition/table.rb', line 37 def children @columns.map(&:child) end |
#column(name, options = {}) ⇒ Object
Add a new method in Table which permit to define data columns
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/active_list/definition/table.rb', line 122 def column(name, = {}) if @model.reflect_on_association(name) [:through] ||= name add :association, name, elsif [:through] && @model.reflect_on_association([:through]) [:label_method] ||= name add :association, name, else add :attribute, name, end end |
#data_columns ⇒ Object
67 68 69 |
# File 'lib/active_list/definition/table.rb', line 67 def data_columns @columns.select { |c| c.is_a? DataColumn } end |
#empty(name, options = {}) ⇒ Object
Add a new method in Table which permit to define empty columns
107 108 109 |
# File 'lib/active_list/definition/table.rb', line 107 def empty(name, = {}) add :empty, name, end |
#exportable_columns ⇒ Object
33 34 35 |
# File 'lib/active_list/definition/table.rb', line 33 def exportable_columns @columns.select(&:exportable?) end |
#global_action_columns ⇒ Object
53 54 55 |
# File 'lib/active_list/definition/table.rb', line 53 def global_action_columns action_columns.select(&:global?) end |
#has_many_records_action? ⇒ Boolean
45 46 47 |
# File 'lib/active_list/definition/table.rb', line 45 def has_many_records_action? many_records_action_columns.any? end |
#hidden_columns ⇒ Object
75 76 77 |
# File 'lib/active_list/definition/table.rb', line 75 def hidden_columns data_columns.select(&:hidden?) end |
#load_default_columns ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/active_list/definition/table.rb', line 140 def load_default_columns for column in model_columns reflections = @model.reflect_on_all_associations(:belongs_to).select { |r| r.foreign_key.to_s == column.name.to_s } if reflections.size == 1 reflection = reflections.first columns = reflection.class_name.constantize.columns.collect { |c| c.name.to_s } column(i[label name code number].detect { |l| columns.include?(l.to_s) }, through: reflection.name, url: true) else column(column.name.to_sym) end end true end |
#many_records_action_columns ⇒ Object
49 50 51 |
# File 'lib/active_list/definition/table.rb', line 49 def many_records_action_columns action_columns.select(&:use_many?) end |
#model_columns ⇒ Object
25 26 27 |
# File 'lib/active_list/definition/table.rb', line 25 def model_columns @model.columns_hash.values end |
#new_column_id ⇒ Object
18 19 20 21 22 23 |
# File 'lib/active_list/definition/table.rb', line 18 def new_column_id @current_column_id ||= 0 id = @current_column_id.to_s # (36).to_sym @current_column_id += 1 id end |
#paginate? ⇒ Boolean
41 42 43 |
# File 'lib/active_list/definition/table.rb', line 41 def paginate? @paginate end |
#reflections ⇒ Object
Compute includes Hash
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/active_list/definition/table.rb', line 80 def reflections hash = [] columns.each do |column| next unless column.respond_to?(:reflection) unless hash.detect { |r| r.name == column.reflection.name } hash << column.reflection end end hash end |
#selectable? ⇒ Boolean
57 58 59 |
# File 'lib/active_list/definition/table.rb', line 57 def selectable? [:selectable] || action_columns.select(&:use_many?).any? || data_columns.any?(&:computable?) end |
#sortable_columns ⇒ Object
29 30 31 |
# File 'lib/active_list/definition/table.rb', line 29 def sortable_columns @columns.select(&:sortable?) end |
#status(*args) ⇒ Object
134 135 136 137 138 |
# File 'lib/active_list/definition/table.rb', line 134 def status(*args) = args. name = args.shift || :status add :status, name, end |
#table_columns ⇒ Object
Retrieves all columns in database
62 63 64 65 |
# File 'lib/active_list/definition/table.rb', line 62 def table_columns cols = model_columns.map(&:name) @columns.select { |c| c.is_a?(DataColumn) && cols.include?(c.name.to_s) } end |
#text_field(name, options = {}) ⇒ Object
Add a new method in Table which permit to define text_field columns
92 93 94 |
# File 'lib/active_list/definition/table.rb', line 92 def text_field(name, = {}) add :text_field, name, end |