Class: Hotsheet::Sheet
Constant Summary collapse
- CONFIG =
{ per: { allowed_classes: [Integer], default: 100 }, as: { allowed_classes: [String, Symbol], default: nil }, scope: { allowed_classes: [Proc], default: nil } }.freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #cells_for(columns, params) ⇒ Object
- #columns ⇒ Object
-
#initialize(name, config = {}, &columns) ⇒ Sheet
constructor
A new instance of Sheet.
- #label ⇒ Object
- #name ⇒ Object
- #scoped ⇒ Object
Methods included from Config
Constructor Details
#initialize(name, config = {}, &columns) ⇒ Sheet
Returns a new instance of Sheet.
14 15 16 17 18 19 20 21 |
# File 'lib/hotsheet/sheet.rb', line 14 def initialize(name, config = {}, &columns) @config = merge_config! CONFIG, config @model = name.to_s.constantize @columns = {} column :id, editable: false columns ? instance_eval(&columns) : use_default_configuration end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/hotsheet/sheet.rb', line 6 def config @config end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
6 7 8 |
# File 'lib/hotsheet/sheet.rb', line 6 def model @model end |
Instance Method Details
#cells_for(columns, params) ⇒ Object
41 42 43 44 45 |
# File 'lib/hotsheet/sheet.rb', line 41 def cells_for(columns, params) page = page_info params[:page] [scoped.order(:id).limit(page[:limit]).offset(page[:offset]).pluck(*columns.keys).transpose, page] end |
#columns ⇒ Object
37 38 39 |
# File 'lib/hotsheet/sheet.rb', line 37 def columns @columns.select { |_name, column| column.visible? } end |
#label ⇒ Object
27 28 29 30 31 |
# File 'lib/hotsheet/sheet.rb', line 27 def label return @model.model_name.human(count: 2) unless @config[:as] @config[:as].to_s.humanize end |
#name ⇒ Object
23 24 25 |
# File 'lib/hotsheet/sheet.rb', line 23 def name (@config[:as] || @model.table_name).to_s end |
#scoped ⇒ Object
33 34 35 |
# File 'lib/hotsheet/sheet.rb', line 33 def scoped @config[:scope] ? @model.instance_exec(&@config[:scope]) : @model end |