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.
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 5 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
106 107 108 109 110 111 112 113 114 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 106 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
126 127 128 129 130 131 132 133 134 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 126 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
116 117 118 119 120 121 122 123 124 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 116 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
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 79 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
93 94 95 96 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 93 def filter(&block) @filter = block if block_given? @filter end |
#item(*args, &block) ⇒ Object
33 34 35 36 37 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 33 def item(*args, &block) item = List::Item.new(@config, *args, block) @items << item item end |
#items ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 39 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
56 57 58 59 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 56 def order(*args) @order = args unless args.empty? @order end |
#page_param_name ⇒ Object
24 25 26 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 24 def page_param_name pagination ? pagination.param_name : nil end |
#pagination(options = nil) ⇒ Object
18 19 20 21 22 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 18 def pagination( = nil) @pagination = List::Pagination.new() unless .nil? @pagination end |
#row_class(record = nil, &block) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 98 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
28 29 30 31 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 28 def sorter(params = nil) @sorter = params if params @sorter end |
#sorter_attribute ⇒ Object
48 49 50 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 48 def sorter_attribute @sorter.keys.first end |
#sorter_direction ⇒ Object
52 53 54 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 52 def sorter_direction @sorter.values.first end |
#title(*args, &block) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 61 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
75 76 77 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 75 def title? @title.present? end |