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
- #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.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/active_list/definition/table.rb', line 8 def initialize(name, model = nil, = {}) @name = name @model = model || name.to_s.classify.constantize @options = @paginate = !(@options[:pagination]==:none || @options[:paginate].is_a?(FalseClass)) @options[:renderer] ||= :simple_renderer @options[:per_page] = 20 if @options[:per_page].to_i <= 0 @options[:page] = 1 if @options[:page].to_i <= 0 @columns = [] @id = ActiveList.new_uid end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
6 7 8 |
# File 'lib/active_list/definition/table.rb', line 6 def columns @columns end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/active_list/definition/table.rb', line 6 def id @id end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
6 7 8 |
# File 'lib/active_list/definition/table.rb', line 6 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/active_list/definition/table.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/active_list/definition/table.rb', line 6 def @options end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
6 7 8 |
# File 'lib/active_list/definition/table.rb', line 6 def parameters @parameters end |
Instance Method Details
#action(name, options = {}) ⇒ Object
Add a new method in Table which permit to define action columns
106 107 108 |
# File 'lib/active_list/definition/table.rb', line 106 def action(name, = {}) add :action, name, end |
#action_columns ⇒ Object
73 74 75 |
# File 'lib/active_list/definition/table.rb', line 73 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
101 102 103 |
# File 'lib/active_list/definition/table.rb', line 101 def check_box(name, = {}) add :check_box, name, end |
#children ⇒ Object
39 40 41 |
# File 'lib/active_list/definition/table.rb', line 39 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] and @model.reflect_on_association([:through]) [:label_method] ||= name add :association, name, else add :attribute, name, end end |
#data_columns ⇒ Object
69 70 71 |
# File 'lib/active_list/definition/table.rb', line 69 def data_columns @columns.select{|c| c.is_a? DataColumn} end |
#exportable_columns ⇒ Object
35 36 37 |
# File 'lib/active_list/definition/table.rb', line 35 def exportable_columns @columns.select{|c| c.exportable?} end |
#global_action_columns ⇒ Object
55 56 57 |
# File 'lib/active_list/definition/table.rb', line 55 def global_action_columns action_columns.select(&:global?) end |
#has_many_records_action? ⇒ Boolean
47 48 49 |
# File 'lib/active_list/definition/table.rb', line 47 def has_many_records_action? many_records_action_columns.any? end |
#hidden_columns ⇒ Object
77 78 79 |
# File 'lib/active_list/definition/table.rb', line 77 def hidden_columns self.data_columns.select(&:hidden?) end |
#load_default_columns ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/active_list/definition/table.rb', line 141 def load_default_columns for column in self.model_columns reflections = @model.reflections.values.select{|r| r.macro == :belongs_to and 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 } self.column([:label, :name, :code, :number].detect{ |l| columns.include?(l.to_s) }, :through => reflection.name, :url => true) else self.column(column.name.to_sym) end end return true end |
#many_records_action_columns ⇒ Object
51 52 53 |
# File 'lib/active_list/definition/table.rb', line 51 def many_records_action_columns action_columns.select(&:use_many?) end |
#model_columns ⇒ Object
27 28 29 |
# File 'lib/active_list/definition/table.rb', line 27 def model_columns @model.columns_hash.values end |
#new_column_id ⇒ Object
20 21 22 23 24 25 |
# File 'lib/active_list/definition/table.rb', line 20 def new_column_id @current_column_id ||= 0 id = @current_column_id.to_s(36).to_sym @current_column_id += 1 return id end |
#paginate? ⇒ Boolean
43 44 45 |
# File 'lib/active_list/definition/table.rb', line 43 def paginate? @paginate end |
#reflections ⇒ Object
Compute includes Hash
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/active_list/definition/table.rb', line 82 def reflections hash = [] self.columns.each do |column| if column.respond_to?(:reflection) unless hash.detect{|r| r.name == column.reflection.name } hash << column.reflection end end end return hash end |
#selectable? ⇒ Boolean
59 60 61 |
# File 'lib/active_list/definition/table.rb', line 59 def selectable? action_columns.select(&:use_many?).any? end |
#sortable_columns ⇒ Object
31 32 33 |
# File 'lib/active_list/definition/table.rb', line 31 def sortable_columns @columns.select{|c| c.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
64 65 66 67 |
# File 'lib/active_list/definition/table.rb', line 64 def table_columns cols = self.model_columns.map(&:name) @columns.select{|c| c.is_a? DataColumn and 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
96 97 98 |
# File 'lib/active_list/definition/table.rb', line 96 def text_field(name, = {}) add :text_field, name, end |