class Presentation::Grid
TODO: ability to render a hash TODO: custom css classes for rows and/or cells TODO: document or complain for required options – id and fields TODO: make fields= accept an ActiveRecord::Base.columns array for a default field set
Attributes
The id for this presentation. Required.
The display title for this presentation. Will default based on the id.
Public Instance Methods
# File lib/presentation/grid.rb, line 38 def colspan @colspan ||= fields.size + (record_links.empty? ? 0 : 1) end
# File lib/presentation/grid.rb, line 34 def fields @fields ||= Presenting::FieldSet.new(Field, :name, :value) end
Paradigm Example:
Grid.new(:fields => [ :email, {"Full Name" => proc{|r| [r.first_name, r.last_name].join(' ')}}, {"Roles" => {:value => :roles, :type => :collection}} ])
Is equivalent to:
g = Grid.new g.fields << :email g.fields << {"Full Name" => proc{|r| [r.first_name, r.last_name].join(' ')}, g.fields << {"Roles" => {:value => :roles, :type => :collection}}
# File lib/presentation/grid.rb, line 28 def fields=(args) args.each do |field| self.fields << field end end
# File lib/presentation/grid.rb, line 42 def iname; :grid end
# File lib/presentation/grid.rb, line 137 def links @links ||= [] end
Links are an area where I almost made the mistake of too much configuration. Presentations are configured in the view, and all of the view helpers are available. When I looked at the (simple) configuration I was building and realized that I could just as easily take the result of link_to, well, I felt a little silly.
Compare:
@grid.links = [ {:name => 'Foo', :url => foo_path, :class => 'foo'} ]
vs:
@grid.links = [ link_to('Foo', foo_path, :class => 'foo') ]
Not only is the second example (the supported example, by the way) shorter and cleaner, it encourages the developer to stay in touch with the Rails internals and therefore discourages a configuration-heavy mindset.
# File lib/presentation/grid.rb, line 131 def links=(set) set.compact.each do |link| raise ArgumentError, "Links must be strings, such as the output of link_to()." unless link.is_a?(String) links << link end end
# File lib/presentation/grid.rb, line 158 def paginate? defined? WillPaginate and (presentable.is_a? WillPaginate::Collection or presentable.respond_to?(:total_entries)) end
# File lib/presentation/grid.rb, line 154 def record_links @record_links ||= [] end
Like links, except the link will appear for each record. This means that the link must be a block that accepts the record as its argument. For example:
@grid.record_links = [
proc{|record| link_to("Foo", foo_path(record), :class => 'foo') }
]
# File lib/presentation/grid.rb, line 148 def record_links=(set) set.compact.each do |link| raise ArgumentError, "Record links must be blocks that accept the record as an argument." unless link.respond_to?(:call) and link.arity == 1 record_links << link end end
# File lib/presentation/grid.rb, line 12 def title @title ||= self.id.titleize end