aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-06-14 15:37:35 +0200
committerTeddy Wing2017-06-14 15:37:35 +0200
commitf327ae88ebc45d291dc8485a0ade20a7e335740f (patch)
tree8192c19285699b14e12e10b5fcd34fea8ebc550f
parentcd510d491181eb7a65559b9bbab600316e4f7b7b (diff)
downloadchouette-core-f327ae88ebc45d291dc8485a0ade20a7e335740f.tar.bz2
TableBuilder: Trial refactor of `#build_links`
Instead of the existing code that we have for `if policy...` etc. in `#build_links`, create a few new methods that ostensibly do the same thing using my `Link` class. It should create a bunch of `Link`s that can be combined with those that come from the `[TABLE-OBJECT].action_links` (like those in `ReferentialDecorator#action_links`). The combination of these will then be turned into `link_to`s by our existing code. Renamed the `actions` argument to `#build_links` to `links` because they come into `table_builder_2` as a `links` variable and I got confused by the name not being the same. The policy checking is now in its own method, `#actions_after_policy_check`. Need to verify that the logic is right, but otherwise it keeps things separate. Might also want to write special methods for each of the conditions between the `||`s. TODO: Move the new private methods to a class. Also test them. Refs #3479
-rw-r--r--app/helpers/table_builder_helper.rb60
1 files changed, 58 insertions, 2 deletions
diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb
index ef44d98c7..f4a3f9eb4 100644
--- a/app/helpers/table_builder_helper.rb
+++ b/app/helpers/table_builder_helper.rb
@@ -119,13 +119,13 @@ module TableBuilderHelper
end
end
- def build_links(item, actions)
+ def build_links(item, links)
trigger = content_tag :div, class: 'btn dropdown-toggle', data: { toggle: 'dropdown' } do
content_tag :span, '', class: 'fa fa-cog'
end
menu = content_tag :ul, class: 'dropdown-menu' do
- item.action_links.map do |link|
+ (action_links(links, item) + item.action_links).map do |link|
content_tag :li, link_to(
link.name,
link.href,
@@ -267,4 +267,60 @@ module TableBuilderHelper
polymorph_url
end
+
+ # TODO: rename
+ def action_links(actions, obj)
+ actions_after_policy_check(actions, obj).map do |action|
+ # TODO: Move that s to another method
+ polymorph_url = []
+
+ unless [:show, :delete].include? action
+ polymorph_url << action
+ end
+
+ polymorph_url += polymorphic_url_parts(obj)
+
+ Link.new(
+ name: t("actions.#{action}"),
+ href: polymorph_url,
+ method: action_link_method(action)
+ )
+ end
+ end
+
+ def action_link_method(action)
+ actions_to_http_methods = {
+ delete: :delete,
+ archive: :put,
+ unarchive: :put
+ }
+
+ actions_to_http_methods[action] || :get
+ end
+
+ def actions_after_policy_check(actions, obj)
+ actions.select do |action|
+ # if action == :delete
+ # if policy(item).present? && policy(item).destroy?
+ # action
+ # end
+ # elsif action == :edit
+ # if policy(item).present? && policy(item).update?
+ # action
+ # end
+ # elsif action == :edit
+ #
+ # end
+ # if (action == :delete && policy(item).present? && policy(item).destroy?) ||
+ (action == :delete && policy(obj).present? && policy(obj).destroy?) ||
+ (action == :delete && !policy(obj).present?) ||
+ (action == :edit && policy(obj).present? && policy(obj).update?) ||
+ (action == :edit && !policy(obj).present?) ||
+ (action == :archive && !obj.archived?) ||
+ (action == :unarchive && obj.archived?) ||
+ ([:delete, :edit, :archive, :unarchive].include?(action))
+ # action
+ # end
+ end
+ end
end