aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-09-01 14:47:29 +0200
committerTeddy Wing2017-09-01 14:47:29 +0200
commit9574d8b8351fd59eeada05a1ba71893a82b86989 (patch)
tree96fc2696e09c85063741ce3a71f44f800d11f127
parentd127ed12158550f84ef1fb9c21d6360c86ac3642 (diff)
downloadchouette-core-9574d8b8351fd59eeada05a1ba71893a82b86989.tar.bz2
TableBuilderHelper::Column: Add `link_to` argument
This parameter will be used as the `href` to link the column value somewhere. We give it a lambda because this makes it easier to call any method on the row object. This means the accessor needs to take the object as an argument, like in the `#value` method, because we don't have a better way to handle that (it can't be done at initialisation time because at that point we don't have row objects, we have a collection).
-rw-r--r--app/helpers/table_builder_helper/column.rb7
-rw-r--r--spec/helpers/table_builder_helper/column_spec.rb16
2 files changed, 22 insertions, 1 deletions
diff --git a/app/helpers/table_builder_helper/column.rb b/app/helpers/table_builder_helper/column.rb
index 800a8282e..ef3c0b629 100644
--- a/app/helpers/table_builder_helper/column.rb
+++ b/app/helpers/table_builder_helper/column.rb
@@ -2,7 +2,7 @@ module TableBuilderHelper
class Column
attr_reader :key, :name, :attribute, :sortable
- def initialize(key: nil, name: '', attribute:, sortable: true)
+ def initialize(key: nil, name: '', attribute:, sortable: true, link_to: nil)
if key.nil? && name.empty?
raise ColumnMustHaveKeyOrNameError
end
@@ -11,6 +11,7 @@ module TableBuilderHelper
@name = name
@attribute = attribute
@sortable = sortable
+ @link_to = link_to
end
def value(obj)
@@ -29,6 +30,10 @@ module TableBuilderHelper
I18n.t("activerecord.attributes.#{model_key}.#{@key}")
end
+
+ def link_to(obj)
+ @link_to.call(obj)
+ end
end
diff --git a/spec/helpers/table_builder_helper/column_spec.rb b/spec/helpers/table_builder_helper/column_spec.rb
index 0f27703b2..0c5c89e8a 100644
--- a/spec/helpers/table_builder_helper/column_spec.rb
+++ b/spec/helpers/table_builder_helper/column_spec.rb
@@ -20,4 +20,20 @@ describe TableBuilderHelper::Column do
).to eq('Numéro de téléphone')
end
end
+
+ describe "#link_to" do
+ it "calls the block passed in and returns the result" do
+ train = double('train', kind: 'TGV')
+
+ expect(
+ TableBuilderHelper::Column.new(
+ name: 'unused',
+ attribute: nil,
+ link_to: lambda do |train|
+ train.kind
+ end
+ ).link_to(train)
+ ).to eq('TGV')
+ end
+ end
end