Class: Hotsheet::Sheet

Inherits:
Object
  • Object
show all
Includes:
Config
Defined in:
lib/hotsheet/sheet.rb

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

Instance Method Summary collapse

Methods included from Config

#merge_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

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/hotsheet/sheet.rb', line 6

def config
  @config
end

#modelObject (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

#columnsObject



37
38
39
# File 'lib/hotsheet/sheet.rb', line 37

def columns
  @columns.select { |_name, column| column.visible? }
end

#labelObject



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

#nameObject



23
24
25
# File 'lib/hotsheet/sheet.rb', line 23

def name
  (@config[:as] || @model.table_name).to_s
end

#scopedObject



33
34
35
# File 'lib/hotsheet/sheet.rb', line 33

def scoped
  @config[:scope] ? @model.instance_exec(&@config[:scope]) : @model
end