diff options
| author | Teddy Wing | 2017-06-12 15:25:58 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2017-06-12 15:25:58 +0200 | 
| commit | 6fb1431b3a2d3649bbd56ebc527fb45803248c94 (patch) | |
| tree | 4b4b71444641be7635f5e4ecf7836f90c8e9c772 /spec | |
| parent | e89aead78b6182a0789d53e46b230df54aee82f3 (diff) | |
| download | chouette-core-6fb1431b3a2d3649bbd56ebc527fb45803248c94.tar.bz2 | |
TableBuilder: Make a new type for columns
Instead of making the columns a hash where the types get all confused
and mussed and we have to type check wherever we use the column value,
make the columns into their own real type.
Adds a new `Column` class to the helper module which lets us interact
with table column data in a much more natural way. Here, when mapping
over the column list, instead of getting a key and value which could be
whatever, we get a real `Column` object that we can call methods on to
get the appropriate data.
It's still not perfect, as evidenced by the `#tbody` method, which has
remained largely the same, but at least we're not checking
`attribute.is_a?(Hash)` like I had tried before.
Update the tests to use the new `TableBuilderHelper::Column` syntax for
defining columns.
Refs #3479
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/helpers/table_builder_helper_spec.rb | 72 | 
1 files changed, 55 insertions, 17 deletions
diff --git a/spec/helpers/table_builder_helper_spec.rb b/spec/helpers/table_builder_helper_spec.rb index 029748dd2..10cd6772b 100644 --- a/spec/helpers/table_builder_helper_spec.rb +++ b/spec/helpers/table_builder_helper_spec.rb @@ -101,15 +101,40 @@ describe TableBuilderHelper, type: :helper do        html_str = helper.table_builder_2(          referentials, -        { :name => 'name', -          :status => Proc.new {|w| w.archived? ? ("<div class='td-block'><span class='fa fa-archive'></span><span>Conservé</span></div>").html_safe : ("<div class='td-block'><span class='sb sb-lg sb-preparing'></span><span>En préparation</span></div>").html_safe}, -          :status => Proc.new {|w| ("<div class='td-block'><span class='sb sb-lg sb-preparing'></span><span>En préparation</span></div>").html_safe}, -          :organisation => Proc.new {|w| w.organisation.name}, -          :validity_period => Proc.new {|w| w.validity_period.nil? ? '-' : t('validity_range', debut: l(w.try(:validity_period).try(:begin), format: :short), end: l(w.try(:validity_period).try(:end), format: :short))}, -          :lines => Proc.new {|w| w.lines.count}, -          :created_at => Proc.new {|w| l(w.created_at, format: :short)}, -          :updated_at => Proc.new {|w| l(w.updated_at, format: :short)}, -          :published_at => ''}, +        [ +          TableBuilderHelper::Column.new( +            key: :name, +            attribute: 'name' +          ), +          TableBuilderHelper::Column.new( +            key: :status, +            attribute: Proc.new {|w| w.archived? ? ("<div class='td-block'><span class='fa fa-archive'></span><span>Conservé</span></div>").html_safe : ("<div class='td-block'><span class='sb sb-lg sb-preparing'></span><span>En préparation</span></div>").html_safe} +          ), +          TableBuilderHelper::Column.new( +            key: :organisation, +            attribute: Proc.new {|w| w.organisation.name} +          ), +          TableBuilderHelper::Column.new( +            key: :validity_period, +            attribute: Proc.new {|w| w.validity_period.nil? ? '-' : t('validity_range', debut: l(w.try(:validity_period).try(:begin), format: :short), end: l(w.try(:validity_period).try(:end), format: :short))} +          ), +          TableBuilderHelper::Column.new( +            key: :lines, +            attribute: Proc.new {|w| w.lines.count} +          ), +          TableBuilderHelper::Column.new( +            key: :created_at, +            attribute: Proc.new {|w| l(w.created_at, format: :short)} +          ), +          TableBuilderHelper::Column.new( +            key: :updated_at, +            attribute: Proc.new {|w| l(w.updated_at, format: :short)} +          ), +          TableBuilderHelper::Column.new( +            key: :published_at, +            attribute: '' +          ) +        ],          selectable: true,          links: [:show, :edit, :archive, :unarchive, :delete],          cls: 'table has-filter has-search' @@ -190,16 +215,29 @@ describe TableBuilderHelper, type: :helper do        html_str = helper.table_builder_2(          companies, -        { -          'ID Codif' => { +        [ +          TableBuilderHelper::Column.new( +            name: 'ID Codif',              attribute: Proc.new { |n| n.try(:objectid).try(:local_id) },              sortable: false -          }, -          :name => 'name', -          :phone => 'phone', -          :email => 'email', -          :url => 'url' -        }, +          ), +          TableBuilderHelper::Column.new( +            key: :name, +            attribute: 'name' +          ), +          TableBuilderHelper::Column.new( +            key: :phone, +            attribute: 'phone' +          ), +          TableBuilderHelper::Column.new( +            key: :email, +            attribute: 'email' +          ), +          TableBuilderHelper::Column.new( +            key: :url, +            attribute: 'url' +          ), +        ],          links: [:show, :edit, :delete],          cls: 'table has-search'        )  | 
