aboutsummaryrefslogtreecommitdiffstats
path: root/app/helpers
diff options
context:
space:
mode:
authorLuc Donnet2018-03-01 16:01:54 +0100
committerGitHub2018-03-01 16:01:54 +0100
commit28b44152c66039a052db8766a61ddfc6566d214d (patch)
treeba6c63e2f271be80a114844947499733c14b6653 /app/helpers
parentc283c5ea57445d5188b18e61b694ba9976bcf0da (diff)
parentb090709fa414939f8c561e9d14bcbb378015c67e (diff)
downloadchouette-core-28b44152c66039a052db8766a61ddfc6566d214d.tar.bz2
Merge pull request #302 from af83/5877-handle-nil-objects-in-tablebuilderhelper
5877 Add a condition on TableBuilderHelper::Column
Diffstat (limited to 'app/helpers')
-rw-r--r--app/helpers/table_builder_helper/column.rb16
1 files changed, 14 insertions, 2 deletions
diff --git a/app/helpers/table_builder_helper/column.rb b/app/helpers/table_builder_helper/column.rb
index 05aa9f563..ff6f2f36f 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