diff options
| author | Zog | 2017-12-20 12:58:20 +0100 | 
|---|---|---|
| committer | Alban Peignier | 2017-12-21 21:19:27 +0100 | 
| commit | 9a34cb48f49df574ae1e7c599713ed246e2938cf (patch) | |
| tree | 49c91cfdaa645f39363c3e8c492b86a0dde08c89 /app | |
| parent | 8cd9b9ee5fc127b2f39a9c89c71327ab8d5e9cec (diff) | |
| download | chouette-core-9a34cb48f49df574ae1e7c599713ed246e2938cf.tar.bz2 | |
Refs #5430 @2h; Deactivate lines instead of destroying them
- Add `activate` and `deactivate` actions in `LinesController`, as well
as corresponding routes
- Add `activate!` and `deactivate!` methods in `Chouette::Line`, as well
as `activated?`
- Add `activate?` and `deactivate?` permissions in `LinePolicy`
- Add corresponding `action_links`in the Decorator
- Create helper for these actions
- Add an optional `'extra_class` to the Links
- Update styles for ".delete-action" to handle the case where there are
several
- Add I18n keys accordingly
Diffstat (limited to 'app')
| -rw-r--r-- | app/assets/stylesheets/components/_buttons.sass | 7 | ||||
| -rw-r--r-- | app/controllers/lines_controller.rb | 8 | ||||
| -rw-r--r-- | app/decorators/line_decorator.rb | 20 | ||||
| -rw-r--r-- | app/helpers/links_helper.rb | 15 | ||||
| -rw-r--r-- | app/helpers/table_builder_helper.rb | 6 | ||||
| -rw-r--r-- | app/models/chouette/line.rb | 11 | ||||
| -rw-r--r-- | app/policies/line_policy.rb | 8 | 
7 files changed, 73 insertions, 2 deletions
| diff --git a/app/assets/stylesheets/components/_buttons.sass b/app/assets/stylesheets/components/_buttons.sass index a59699383..a649a07ef 100644 --- a/app/assets/stylesheets/components/_buttons.sass +++ b/app/assets/stylesheets/components/_buttons.sass @@ -165,6 +165,13 @@ table, .table              .fa:first-child                margin-right: 0.5em +          & + li.delete-action +            > a, > button +              margin-top: 0 +              &:before +                display: none + +    &.table-2entries .t2e-item      > .th        position: relative diff --git a/app/controllers/lines_controller.rb b/app/controllers/lines_controller.rb index 571c73f4a..676581076 100644 --- a/app/controllers/lines_controller.rb +++ b/app/controllers/lines_controller.rb @@ -50,6 +50,14 @@ class LinesController < ChouetteController      super    end +  %w(activate deactivate).each do |action| +    define_method action do +      authorize resource, "#{action}?" +      resource.send "#{action}!" +      redirect_to request.referer || [resource.line_referential, resource] +    end +  end +    # overwrite inherited resources to use delete instead of destroy    # foreign keys will propagate deletion)    def destroy_resource(object) diff --git a/app/decorators/line_decorator.rb b/app/decorators/line_decorator.rb index ede670cbd..fe0750b05 100644 --- a/app/decorators/line_decorator.rb +++ b/app/decorators/line_decorator.rb @@ -41,6 +41,26 @@ class LineDecorator < Draper::Decorator        )      end +    if h.policy(object).deactivate? +      links << Link.new( +        content: h.deactivate_link_content('lines.actions.deactivate'), +        href: h.deactivate_line_referential_line_path(context[:line_referential], object), +        method: :put, +        data: {confirm: h.t('lines.actions.deactivate_confirm')}, +        extra_class: "delete-action" +      ) +    end + +    if h.policy(object).activate? +      links << Link.new( +        content: h.activate_link_content('lines.actions.activate'), +        href: h.activate_line_referential_line_path(context[:line_referential], object), +        method: :put, +        data: {confirm: h.t('lines.actions.activate_confirm')}, +        extra_class: "delete-action" +      ) +    end +      if h.policy(object).destroy?        links << Link.new(          content: h.destroy_link_content('lines.actions.destroy'), diff --git a/app/helpers/links_helper.rb b/app/helpers/links_helper.rb index 4fb7a797d..088415dc3 100644 --- a/app/helpers/links_helper.rb +++ b/app/helpers/links_helper.rb @@ -1,5 +1,18 @@  module LinksHelper +  def custom_link_content(translation_key, klass, extra_class: nil) +    klass = ["fa", "fa-#{klass}", "mr-xs", extra_class].compact.join(" ") +    content_tag(:span, nil, class: klass) + t(translation_key) +  end +    def destroy_link_content(translation_key = 'actions.destroy') -    content_tag(:span, nil, class: 'fa fa-trash mr-xs') + t(translation_key) +    custom_link_content translation_key, 'trash' +  end + +  def deactivate_link_content(translation_key = 'actions.deactivate') +    custom_link_content translation_key, 'power-off', extra_class: "text-danger" +  end + +  def activate_link_content(translation_key = 'actions.activate') +    custom_link_content translation_key, 'power-off', extra_class: "text-success"    end  end diff --git a/app/helpers/table_builder_helper.rb b/app/helpers/table_builder_helper.rb index de78e903d..96b2889da 100644 --- a/app/helpers/table_builder_helper.rb +++ b/app/helpers/table_builder_helper.rb @@ -368,6 +368,10 @@ module TableBuilderHelper    end    def gear_menu_link(link) +    klass = [link.extra_class] +    klass << 'delete-action' if link.method == :delete +    klass = klass.compact.join(' ') +    klass = nil unless klass.present?      content_tag(        :li,        link_to( @@ -377,7 +381,7 @@ module TableBuilderHelper        ) do          link.content        end, -      class: ('delete-action' if link.method == :delete) +      class: klass      )    end diff --git a/app/models/chouette/line.rb b/app/models/chouette/line.rb index 93d4f5e8b..2d776e94b 100644 --- a/app/models/chouette/line.rb +++ b/app/models/chouette/line.rb @@ -79,5 +79,16 @@ module Chouette        line_referential.companies.where(id: ([company_id] + Array(secondary_company_ids)).compact)      end +    def deactivate! +      update_attribute :deactivated, true +    end + +    def activate! +      update_attribute :deactivated, false +    end + +    def activated? +      !deactivated +    end    end  end diff --git a/app/policies/line_policy.rb b/app/policies/line_policy.rb index 67ea0b611..e5674fbe2 100644 --- a/app/policies/line_policy.rb +++ b/app/policies/line_policy.rb @@ -14,6 +14,14 @@ class LinePolicy < ApplicationPolicy      user.has_permission?('lines.destroy')    end +  def deactivate? +    !record.deactivated? && user.has_permission?('lines.change_status') +  end + +  def activate? +    record.deactivated? && user.has_permission?('lines.change_status') +  end +    def update?      user.has_permission?('lines.update')    end | 
