Class: DynamicScaffold::ListBuilder
- Inherits:
-
Object
- Object
- DynamicScaffold::ListBuilder
- Defined in:
- lib/dynamic_scaffold/list_builder.rb
Instance Attribute Summary collapse
- #add_button(&block) ⇒ Object
- #destroy_buttons(record = nil, &block) ⇒ Object
- #edit_buttons(record = nil, &block) ⇒ Object
Instance Method Summary collapse
- #build_sql(scope_params) ⇒ Object
- #filter(&block) ⇒ Object
-
#initialize(config) ⇒ ListBuilder
constructor
A new instance of ListBuilder.
- #item(*args, &block) ⇒ Object
- #items ⇒ Object
- #order(*args) ⇒ Object
- #page_param_name ⇒ Object
- #pagination(options = nil) ⇒ Object
- #row_class(record = nil, &block) ⇒ Object
- #sorter(params = nil) ⇒ Object
- #sorter_attribute ⇒ Object
- #sorter_direction ⇒ Object
- #title(*args, &block) ⇒ Object
- #title? ⇒ Boolean
Constructor Details
#initialize(config) ⇒ ListBuilder
Returns a new instance of ListBuilder.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 7 def initialize(config) @config = config @items = [] @sorter = nil @order = [] @title = nil @filter = nil @row_class_block = nil @add_button = true @edit_buttons = true @destroy_buttons = true end |
Instance Attribute Details
#add_button(&block) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 108 def (&block) if block_given? @add_button_block = block elsif @add_button_block @config.controller.view_context.instance_exec(&@add_button_block) else @add_button end end |
#destroy_buttons(record = nil, &block) ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 128 def (record = nil, &block) if block_given? @destroy_buttons_block = block elsif record.present? && @destroy_buttons_block @config.controller.view_context.instance_exec(record, &@destroy_buttons_block) else @destroy_buttons end end |
#edit_buttons(record = nil, &block) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 118 def (record = nil, &block) if block_given? @edit_buttons_block = block elsif record.present? && @edit_buttons_block @config.controller.view_context.instance_exec(record, &@edit_buttons_block) else @edit_buttons end end |
Instance Method Details
#build_sql(scope_params) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 81 def build_sql(scope_params) sql = @config.model.all sql = sql.where scope_params ret = @config.controller.instance_exec(sql, &@filter) unless @filter.nil? sql = ret unless ret.nil? unless sql.is_a? ::ActiveRecord::Relation raise( Error::InvalidOperation, 'You must return ActiveRecord::Relation from filter block' ) end sql end |
#filter(&block) ⇒ Object
95 96 97 98 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 95 def filter(&block) @filter = block if block_given? @filter end |
#item(*args, &block) ⇒ Object
35 36 37 38 39 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 35 def item(*args, &block) item = List::Item.new(@config, *args, block) @items << item item end |
#items ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 41 def items if @items.empty? @config.model.column_names.each do |column| @items << List::Item.new(@config, column, nil) end end @items end |
#order(*args) ⇒ Object
58 59 60 61 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 58 def order(*args) @order = args unless args.empty? @order end |
#page_param_name ⇒ Object
26 27 28 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 26 def page_param_name pagination ? pagination.param_name : nil end |
#pagination(options = nil) ⇒ Object
20 21 22 23 24 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 20 def pagination( = nil) @pagination = List::Pagination.new() unless .nil? @pagination end |
#row_class(record = nil, &block) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 100 def row_class(record = nil, &block) if block_given? @row_class_block = block elsif record.present? && @row_class_block @config.controller.view_context.instance_exec(record, &@row_class_block) end end |
#sorter(params = nil) ⇒ Object
30 31 32 33 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 30 def sorter(params = nil) @sorter = params if params @sorter end |
#sorter_attribute ⇒ Object
50 51 52 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 50 def sorter_attribute @sorter.keys.first end |
#sorter_direction ⇒ Object
54 55 56 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 54 def sorter_direction @sorter.values.first end |
#title(*args, &block) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 63 def title(*args, &block) if args[0].is_a?(Symbol) || args[0].is_a?(String) || block_given? @title = { column_name: args[0], block: block } else record = args[0] return @config.controller.view_context.instance_exec(record, &@title[:block]) if @title[:block] record.public_send(@title[:column_name]) end end |
#title? ⇒ Boolean
77 78 79 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 77 def title? @title.present? end |