From 71611d2d4ffd5a1d6569acd0cae703ed22c55ed5 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 8 Jun 2017 14:29:11 +0200 Subject: TableBuilder: Port `links_builder` and `sortable_columns` from old code Bring these two methods from `NewapplicationHelper` into `TableBuilderHelper` so we can mess with them in the context of our helper. We'll want to try to change & improve them a bit. Also need to make sure we rename them, otherwise they'll conflict with the existing helper methods. Added a few notes to myself in the comments. Refs #3479 --- app/helpers/table_builder_helper.rb | 100 ++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb index 7b8ada9a2..54f6833e7 100644 --- a/app/helpers/table_builder_helper.rb +++ b/app/helpers/table_builder_helper.rb @@ -100,4 +100,104 @@ module TableBuilderHelper content_tag :table, head + body, class: cls end + + private + + # TODO: `def build_link[s]` + def links_builder(item, actions) + 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 + actions.collect do |action| + polymorph_url = [] + + unless [:show, :delete].include? action + polymorph_url << action + end + + unless item.class == Calendar or item.class == Referential + if current_referential + polymorph_url << current_referential + polymorph_url << item.line if item.respond_to? :line + polymorph_url << item.route.line if item.class == Chouette::RoutingConstraintZone + polymorph_url << item if item.respond_to? :line_referential + polymorph_url << item.stop_area if item.respond_to? :stop_area + polymorph_url << item if item.respond_to? :stop_points or item.class.to_s == 'Chouette::TimeTable' + elsif item.respond_to? :referential + polymorph_url << item.referential + end + else + polymorph_url << item + end + + if action == :delete + if policy(item).present? + if policy(item).destroy? + content_tag :li, '', class: 'delete-action' do + link_to(polymorph_url, method: :delete, data: { confirm: 'Etes-vous sûr(e) de vouloir effectuer cette action ?' }) do + txt = t("actions.#{action}") + pic = content_tag :span, '', class: 'fa fa-trash' + pic + txt + end + end + end + else + content_tag :li, '', class: 'delete-action' do + link_to(polymorph_url, method: :delete, data: { confirm: 'Etes-vous sûr(e) de vouloir effectuer cette action ?' }) do + txt = t("actions.#{action}") + pic = content_tag :span, '', class: 'fa fa-trash' + pic + txt + end + end + end + + elsif action == :edit + if policy(item).present? + if policy(item).update? + content_tag :li, link_to(t("actions.#{action}"), polymorph_url) + end + else + content_tag :li, link_to(t("actions.#{action}"), polymorph_url) + end + elsif action == :archive + unless item.archived? + content_tag :li, link_to(t("actions.#{action}"), polymorph_url, method: :put) + end + elsif action == :unarchive + if item.archived? + content_tag :li, link_to(t("actions.#{action}"), polymorph_url, method: :put) + end + else + content_tag :li, link_to(t("actions.#{action}"), polymorph_url) + end + end.join.html_safe + end + + content_tag :div, trigger + menu, class: 'btn-group' + + end + + # TODO: clean up? + def sortable_columns collection, key + # #, #]> + # (byebug) collection.model + # Referential(id: integer, name: string, slug: string, created_at: datetime, updated_at: datetime, prefix: string, projection_type: string, time_zone: string, bounds: string, organisation_id: integer, geographical_bounds: text, user_id: integer, user_name: string, data_format: string, line_referential_id: integer, stop_area_referential_id: integer, workbench_id: integer, archived_at: datetime, created_from_id: integer, ready: boolean) + # params = {"controller"=>"workbenches", "action"=>"show", "id"=>"1", "q"=>{"archived_at_not_null"=>"1", "archived_at_null"=>"1"}} + direction = (key.to_s == params[:sort] && params[:direction] == 'desc') ? 'asc' : 'desc' + + link_to(params.merge({direction: direction, sort: key})) do + pic1 = content_tag :span, '', class: "fa fa-sort-asc #{(direction == 'desc') ? 'active' : ''}" + pic2 = content_tag :span, '', class: "fa fa-sort-desc #{(direction == 'asc') ? 'active' : ''}" + + pics = content_tag :span, pic1 + pic2, class: 'orderers' + # This snake cases and downcases the class name. Should use the ActiveSupport method to do this + # TODO: Maybe give this whole thing a name so it's clearer what's going on. Also, figure out a way to maybe explicitise the dynamicness of getting the model type from the `collection`. + # TODO: move these two lines to a new method called `column_header_label` and rename `pics` to something like `icons` or arrow icons or some such + obj = collection.model.to_s.gsub('Chouette::', '').scan(/[A-Z][a-z]+/).join('_').downcase + + (I18n.t("activerecord.attributes.#{obj}.#{key}") + pics).html_safe + end + end end -- cgit v1.2.3