aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorZog2018-02-12 11:18:10 +0100
committerZog2018-02-12 11:18:10 +0100
commitb090709fa414939f8c561e9d14bcbb378015c67e (patch)
treee9dd671c94ecbfc3735fd86c89deb2a834b30eee /app
parent7f044f8ab9cf9394c4180f3d467a74bd95dcd398 (diff)
downloadchouette-core-b090709fa414939f8c561e9d14bcbb378015c67e.tar.bz2
Refs #5877; Add a condition on TableBuilderHelper::Column5877-handle-nil-objects-in-tablebuilderhelper
Diffstat (limited to 'app')
-rw-r--r--app/helpers/table_builder_helper/column.rb16
-rw-r--r--app/views/compliance_check_sets/index.html.slim5
2 files changed, 17 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( \