Modify implementation of how data tables define cells

* We want a clean, clear, but flexible interface for using data tables
  * We want the model to define our cell
  * We want to be informative to the user if they do not define the cell
This commit is contained in:
David Larrabee
2015-04-04 08:16:00 -04:00
parent 847ef63128
commit 7eb232675f
4 changed files with 55 additions and 67 deletions

View File

@@ -1,12 +1,17 @@
module ProMotion
class DataTableScreen < TableScreen
class << self
def model(value, scope=nil)
if value.method_defined?(:cell)
@data_model = value
@data_scope = scope || :all
else
raise "#{value} must define the cell method"
end
end
def self.cell(cell)
@cell = cell
end
def self.cell_properties
@cell
def data_model; @data_model; end
def data_scope; @data_scope; end
end
def table_data
@@ -15,47 +20,20 @@ module ProMotion
}]
end
def cell_properties
self.class.cell_properties
end
def cell_data
return [] if cell_properties.nil?
return [] if data_model.nil?
data_model.send(data_scope).collect do |c|
if c.respond_to?(:cell)
c.cell
else
{
cell_class: cell_class,
properties: properties.inject({}) do |hash, element|
hash[element.first] = c.send(element.last)
hash
end
}
end
end
data_model.send(data_scope).collect(&:cell)
end
private
def properties
@properties ||= cell_properties[:template].reject do |k,v|
k == :cell_class
end unless cell_properties[:template].nil?
end
def cell_class
@cell_class ||= cell_properties[:template].nil? ? nil : cell_properties[:template][:cell_class]
end
def data_model
@data_model ||= cell_properties[:model]
self.class.data_model
end
def data_scope
@data_scope ||= cell_properties[:scope] || :all
self.class.data_scope
end
end
end