Class: DynamicScaffold::ListBuilder
- Inherits:
-
Object
- Object
- DynamicScaffold::ListBuilder
- Defined in:
- lib/dynamic_scaffold/list_builder.rb
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.
3 4 5 6 7 8 9 10 11 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 3 def initialize(config) @config = config @items = [] @sorter = nil @order = [] @title = nil @filter = nil @row_class_block = nil end |
Instance Method Details
#build_sql(scope_params) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 74 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
88 89 90 91 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 88 def filter(&block) @filter = block if block_given? @filter end |
#item(*args, &block) ⇒ Object
28 29 30 31 32 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 28 def item(*args, &block) item = List::Item.new(@config, *args, block) @items << item item end |
#items ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 34 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
51 52 53 54 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 51 def order(*args) @order = args unless args.empty? @order end |
#page_param_name ⇒ Object
19 20 21 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 19 def page_param_name pagination ? pagination.param_name : nil end |
#pagination(options = nil) ⇒ Object
13 14 15 16 17 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 13 def pagination( = nil) @pagination = List::Pagination.new() unless .nil? @pagination end |
#row_class(record = nil, &block) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 93 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
23 24 25 26 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 23 def sorter(params = nil) @sorter = params if params @sorter end |
#sorter_attribute ⇒ Object
43 44 45 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 43 def sorter_attribute @sorter.keys.first end |
#sorter_direction ⇒ Object
47 48 49 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 47 def sorter_direction @sorter.values.first end |
#title(*args, &block) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 56 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
70 71 72 |
# File 'lib/dynamic_scaffold/list_builder.rb', line 70 def title? @title.present? end |