aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/helpers/table_builder_helper/column.rb16
-rw-r--r--app/views/compliance_check_sets/index.html.slim5
-rw-r--r--spec/helpers/table_builder_helper_spec.rb47
3 files changed, 64 insertions, 4 deletions
diff --git a/app/helpers/table_builder_helper/column.rb b/app/helpers/table_builder_helper/column.rb
index b4c569882..70a8e29a4 100644
--- a/app/helpers/table_builder_helper/column.rb
+++ b/app/helpers/table_builder_helper/column.rb
@@ -2,19 +2,21 @@ module TableBuilderHelper
class Column
attr_reader :key, :name, :attribute, :sortable
- def initialize(key: nil, name: '', attribute:, sortable: true, link_to: nil)
+ def initialize(key: nil, name: '', attribute:, sortable: true, link_to: nil, **opts)
if key.nil? && name.empty?
raise ColumnMustHaveKeyOrNameError
end
-
+ opts ||= {}
@key = key
@name = name
@attribute = attribute
@sortable = sortable
@link_to = link_to
+ @condition = opts[:if]
end
def value(obj)
+ return unless check_condition(obj)
if @attribute.is_a?(Proc)
@attribute.call(obj)
else
@@ -36,8 +38,18 @@ module TableBuilderHelper
end
def link_to(obj)
+ return unless check_condition(obj)
@link_to.call(obj)
end
+
+ def check_condition(obj)
+ condition_val = true
+ if @condition.present?
+ condition_val = @condition
+ condition_val = condition_val.call(obj) if condition_val.is_a?(Proc)
+ end
+ !!condition_val
+ end
end
diff --git a/app/views/compliance_check_sets/index.html.slim b/app/views/compliance_check_sets/index.html.slim
index 31ad31e5b..2b5e5cb2d 100644
--- a/app/views/compliance_check_sets/index.html.slim
+++ b/app/views/compliance_check_sets/index.html.slim
@@ -23,9 +23,10 @@
), \
TableBuilderHelper::Column.new( \
key: :associated_object, \
- attribute: Proc.new{|n| n.referential.present? ? n.referential.name : ''}, \
+ if: ->(compliance_check_set){ compliance_check_set.referential.present? }, \
+ attribute: Proc.new{|n| n.referential.name}, \
link_to: lambda do |compliance_check_set| \
- compliance_check_set.referential.present? ? referential_path(compliance_check_set.referential_id) : '#' \
+ referential_path(compliance_check_set.referential_id) \
end \
), \
TableBuilderHelper::Column.new( \
diff --git a/spec/helpers/table_builder_helper_spec.rb b/spec/helpers/table_builder_helper_spec.rb
index 5bddbb16f..aff63ec59 100644
--- a/spec/helpers/table_builder_helper_spec.rb
+++ b/spec/helpers/table_builder_helper_spec.rb
@@ -435,6 +435,53 @@ describe TableBuilderHelper, type: :helper do
allow(helper).to receive(:current_user).and_return(user_context)
}
+ context "with a condition" do
+ let(:columns){
+ [
+ TableBuilderHelper::Column.new(
+ key: :name,
+ attribute: 'name',
+ if: condition
+ ),
+ ]
+ }
+
+ context "when the condition is true" do
+ let(:condition){ ->(obj){true} }
+ it "should show the value" do
+ items.each do |i|
+ tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name, :index)
+ klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{i.id}"
+ expect(tr).to include(i.name)
+ end
+ end
+ end
+
+ context "when the condition depends on the object" do
+ let(:condition){ ->(obj){ obj == referential } }
+ it "should show the value accordingly" do
+ tr = helper.send(:tr, item, columns, selectable, links, overhead, model_name, :index)
+ klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{referential.id}"
+ expect(tr).to include(referential.name)
+ tr = helper.send(:tr, other_item, columns, selectable, links, overhead, model_name, :index)
+ klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{other_referential.id}"
+ expect(tr).to_not include(other_referential.name)
+ end
+ end
+
+ context "when the condition is false" do
+ let(:condition){ ->(obj){false} }
+ it "should not show the value" do
+ items.each do |i|
+ tr = helper.send(:tr, i, columns, selectable, links, overhead, model_name, :index)
+ klass = "#{TableBuilderHelper.item_row_class_name([referential])}-#{i.id}"
+ expect(tr).to_not include(i.name)
+ end
+ end
+ end
+
+ end
+
context "with all rows non-selectable" do
let(:selectable){ false }
it "sets all rows as non selectable" do