Class: Presentation::Grid

Inherits:
Base
  • Object
show all
Defined in:
lib/presentation/grid.rb

Overview

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

Direct Known Subclasses

Details

Defined Under Namespace

Classes: Field

Instance Attribute Summary collapse

Attributes inherited from Base

#controller, #presentable

Instance Method Summary collapse

Methods inherited from Base

#render

Methods included from Presenting::Configurable

#initialize

Instance Attribute Details

#idObject

The id for this presentation. Required.



8
9
10
# File 'lib/presentation/grid.rb', line 8

def id
  @id
end

#titleObject



12
13
14
# File 'lib/presentation/grid.rb', line 12

def title
  @title ||= self.id.titleize
end

Instance Method Details

#colspanObject



38
39
40
# File 'lib/presentation/grid.rb', line 38

def colspan
  @colspan ||= fields.size + (record_links.empty? ? 0 : 1)
end

#fieldsObject



34
35
36
# File 'lib/presentation/grid.rb', line 34

def fields
  @fields ||= Presenting::FieldSet.new(Field, :name, :value)
end

#fields=(args) ⇒ Object

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}}


28
29
30
31
32
# File 'lib/presentation/grid.rb', line 28

def fields=(args)
  args.each do |field|
    self.fields << field
  end
end

#inameObject



42
# File 'lib/presentation/grid.rb', line 42

def iname; :grid end


137
138
139
# File 'lib/presentation/grid.rb', line 137

def links
  @links ||= []
end

#links=(set) ⇒ Object

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.



131
132
133
134
135
136
# 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

#paginate?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/presentation/grid.rb', line 158

def paginate?
  defined? WillPaginate and (presentable.is_a? WillPaginate::Collection or presentable.respond_to?(:total_entries))
end


154
155
156
# File 'lib/presentation/grid.rb', line 154

def record_links
  @record_links ||= []
end

#record_links=(set) ⇒ Object

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:

]



148
149
150
151
152
153
# 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