diff options
| author | Zog | 2018-01-17 11:10:29 +0100 |
|---|---|---|
| committer | Zog | 2018-01-25 17:18:00 +0100 |
| commit | 69d1f47e21fea3c151eabafcdd9051d9d10e92a3 (patch) | |
| tree | 60c8176c48aedf7b204f2a20d4717b7a58171a2b | |
| parent | fc462f368bc45143b9f0b8649e5bec1f69e414c7 (diff) | |
| download | chouette-core-69d1f47e21fea3c151eabafcdd9051d9d10e92a3.tar.bz2 | |
Refs #5586; Add shortcut actions and refactor already migrated decorators
| -rw-r--r-- | app/decorators/calendar_decorator.rb | 24 | ||||
| -rw-r--r-- | app/decorators/company_decorator.rb | 12 | ||||
| -rw-r--r-- | app/decorators/line_decorator.rb | 19 | ||||
| -rw-r--r-- | app/decorators/referential_decorator.rb | 45 | ||||
| -rw-r--r-- | app/decorators/time_table_decorator.rb | 22 | ||||
| -rw-r--r-- | lib/af83/enhanced_decorator.rb | 54 |
6 files changed, 81 insertions, 95 deletions
diff --git a/app/decorators/calendar_decorator.rb b/app/decorators/calendar_decorator.rb index c47e4d1d5..be1f9e3bf 100644 --- a/app/decorators/calendar_decorator.rb +++ b/app/decorators/calendar_decorator.rb @@ -1,26 +1,12 @@ class CalendarDecorator < AF83::Decorator decorates Calendar - - action_link on: :index, primary: :index, policy: :create do |l| - l.content { h.t('actions.add') } - l.href { h.new_calendar_path } - end - - with_instance_decorator do |instance_decorator| - instance_decorator.action_link primary: :index do |l| - l.content { h.t('actions.show') } - l.href { [object] } - end - instance_decorator.action_link primary: %i(show index) do |l| - l.content { h.t('actions.edit') } - l.href { [object] } - end + create_action_link - instance_decorator.action_link policy: :destroy, footer: true, secondary: :show do |l| - l.content { h.destroy_link_content } - l.href { h.calendar_path(object) } - l.method { :delete } + with_instance_decorator do |instance_decorator| + instance_decorator.show_action_link + instance_decorator.edit_action_link + instance_decorator.destroy_action_link do |l| l.data {{ confirm: h.t('calendars.actions.destroy_confirm') }} end end diff --git a/app/decorators/company_decorator.rb b/app/decorators/company_decorator.rb index 631e030db..3737b851e 100644 --- a/app/decorators/company_decorator.rb +++ b/app/decorators/company_decorator.rb @@ -1,18 +1,15 @@ class CompanyDecorator < AF83::Decorator decorates Chouette::Company - action_link on: :index, primary: :index, policy: :create do |l| + create_action_link do |l| l.content { h.t('companies.actions.new') } l.href { [:new, context[:referential], :company] } end with_instance_decorator do |instance_decorator| - instance_decorator.action_link primary: :index do |l| - l.content { h.t('actions.show') } - l.href { [object] } - end + instance_decorator.show_action_link - instance_decorator.action_link primary: %i(show index) do |l| + instance_decorator.edit_action_link do |l| l.content {|l| l.action == "show" ? h.t('actions.edit') : h.t('companies.actions.edit') } l.href { h.edit_line_referential_company_path( @@ -22,7 +19,7 @@ class CompanyDecorator < AF83::Decorator } end - instance_decorator.action_link policy: :destroy, footer: true, secondary: :show do |l| + instance_decorator.destroy_action_link do |l| l.content { h.destroy_link_content('companies.actions.destroy') } l.href { h.edit_line_referential_company_path( @@ -30,7 +27,6 @@ class CompanyDecorator < AF83::Decorator object ) } - l.method { :delete } l.data {{ confirm: h.t('companies.actions.destroy_confirm') }} end end diff --git a/app/decorators/line_decorator.rb b/app/decorators/line_decorator.rb index a8623e7d0..d8ffdad69 100644 --- a/app/decorators/line_decorator.rb +++ b/app/decorators/line_decorator.rb @@ -1,8 +1,8 @@ class LineDecorator < AF83::Decorator decorates Chouette::Line - action_link on: :index, primary: :index, policy: :create do |l| - l.content { h.t('lines.actions.new') } + create_action_link do |l| + l.content t('lines.actions.new') l.href { h.new_line_referential_line_path(context[:line_referential]) } end @@ -12,18 +12,18 @@ class LineDecorator < AF83::Decorator ### - an array of actions ### - a boolean - instance_decorator.action_link primary: :index, on: :index do |l| - l.content h.t('lines.actions.show') + instance_decorator.show_action_link do |l| + l.content t('lines.actions.show') l.href { [context[:line_referential], object] } end instance_decorator.action_link do |l| - l.content h.t('lines.actions.show_network') + l.content t('lines.actions.show_network') l.href { [context[:line_referential], object.network] } end instance_decorator.action_link do |l| - l.content { h.t('lines.actions.show_company') } + l.content t('lines.actions.show_company') l.href { [context[:line_referential], object.company] } l.disabled { object.company.nil? } end @@ -31,13 +31,13 @@ class LineDecorator < AF83::Decorator can_edit_line = ->(){ h.policy(Chouette::Line).create? && context[:line_referential].organisations.include?(context[:current_organisation]) } instance_decorator.with_condition can_edit_line do - action_link on: %i(index show), primary: :show, secondary: :index do |l| + edit_action_link do |l| l.content {|l| l.primary? ? h.t('actions.edit') : h.t('lines.actions.edit') } l.href { h.edit_line_referential_line_path(context[:line_referential], object.id) } end action_link on: :index, secondary: :index do |l| - l.content { h.t('lines.actions.new') } + l.content t('lines.actions.new') l.href { h.new_line_referential_line_path(context[:line_referential]) } end end @@ -61,10 +61,9 @@ class LineDecorator < AF83::Decorator l.extra_class "delete-action" end - instance_decorator.action_link policy: :destroy, footer: true do |l| + instance_decorator.destroy_action_link do |l| l.content { h.destroy_link_content('lines.actions.destroy') } l.href { h.line_referential_line_path(context[:line_referential], object) } - l.method :delete l.data confirm: h.t('lines.actions.destroy_confirm') l.extra_class "delete-action" end diff --git a/app/decorators/referential_decorator.rb b/app/decorators/referential_decorator.rb index ebde97671..226f8269e 100644 --- a/app/decorators/referential_decorator.rb +++ b/app/decorators/referential_decorator.rb @@ -2,10 +2,7 @@ class ReferentialDecorator < AF83::Decorator decorates Referential with_instance_decorator do |instance_decorator| - instance_decorator.action_link primary: %i(show index) do |l| - l.content { h.t('actions.edit') } - l.href { [object] } - end + instance_decorator.edit_action_link instance_decorator.action_link feature: :referential_vehicle_journeys, secondary: :show do |l| l.content t('referential_vehicle_journeys.index.title') @@ -54,47 +51,9 @@ class ReferentialDecorator < AF83::Decorator }} end - instance_decorator.action_link policy: :destroy, footer: true, secondary: :show do |l| - l.content { h.destroy_link_content } + instance_decorator.destroy_action_link do |l| l.href { h.referential_path(object) } - l.method { :delete } l.data {{ confirm: h.t('referentials.actions.destroy_confirm') }} end - end - # def action_links - # policy = h.policy(object) - # links = [] - # - # if policy.edit? - # links << HTMLElement.new( - # :button, - # 'Purger', - # type: 'button', - # data: { - # toggle: 'modal', - # target: '#purgeModal' - # } - # ) - # end - # - # if policy.destroy? - # links << Link.new( - # content: h.destroy_link_content, - # href: h.referential_path(object), - # method: :delete, - # data: { confirm: h.t('referentials.actions.destroy_confirm') } - # ) - # end - - # links - # end - # - # private - # - # # TODO move to a base Decorator (ApplicationDecorator) - # def has_feature?(*features) - # h.has_feature?(*features) rescue false - # end - end diff --git a/app/decorators/time_table_decorator.rb b/app/decorators/time_table_decorator.rb index fc808f091..d3aef4550 100644 --- a/app/decorators/time_table_decorator.rb +++ b/app/decorators/time_table_decorator.rb @@ -1,25 +1,21 @@ class TimeTableDecorator < AF83::Decorator decorates Chouette::TimeTable - action_link on: :index, primary: :index, \ - if: ->{ h.policy(Chouette::TimeTable).create? && context[:referential].organisation == h.current_organisation } do |l| - l.content { h.t('actions.add') } - l.href { h.new_referential_time_table_path(context[:referential]) } + create_action_link if: ->{ h.policy(Chouette::TimeTable).create? && context[:referential].organisation == h.current_organisation } do |l| + l.href { h.new_referential_time_table_path(context[:referential]) } end with_instance_decorator do |instance_decorator| - instance_decorator.action_link primary: :index do |l| - l.content { h.t('actions.show') } + instance_decorator.show_action_link do |l| l.href { [context[:referential], object] } end - instance_decorator.action_link primary: %i(show index) do |l| - l.content { h.t('actions.edit') } + instance_decorator.edit_action_link do |l| l.href { [context[:referential], object] } end instance_decorator.action_link if: ->{ object.calendar }, secondary: true do |l| - l.content { h.t('actions.actualize') } + l.content t('actions.actualize') l.href do h.actualize_referential_time_table_path( context[:referential], @@ -30,7 +26,7 @@ class TimeTableDecorator < AF83::Decorator end instance_decorator.action_link policy: :edit, secondary: true do |l| - l.content { h.t('actions.combine') } + l.content t('actions.combine') l.href do h.new_referential_time_table_time_table_combination_path( context[:referential], @@ -40,7 +36,7 @@ class TimeTableDecorator < AF83::Decorator end instance_decorator.action_link policy: :duplicate, secondary: true do |l| - l.content { h.t('actions.duplicate') } + l.content t('actions.duplicate') l.href do h.duplicate_referential_time_table_path( context[:referential], @@ -49,15 +45,13 @@ class TimeTableDecorator < AF83::Decorator end end - instance_decorator.action_link policy: :destroy, footer: true, secondary: :show do |l| - l.content { h.destroy_link_content } + instance_decorator.destroy_action_link do |l| l.href do h.duplicate_referential_time_table_path( context[:referential], object ) end - l.method { :delete } l.data {{ confirm: h.t('time_tables.actions.destroy_confirm') }} end end diff --git a/lib/af83/enhanced_decorator.rb b/lib/af83/enhanced_decorator.rb index b4e679164..ee18686f4 100644 --- a/lib/af83/enhanced_decorator.rb +++ b/lib/af83/enhanced_decorator.rb @@ -7,6 +7,7 @@ module AF83::EnhancedDecorator options, link_options = parse_options args link = AF83::Decorator::Link.new(link_options) + instance_exec(link, &options[:before_block]) if options[:before_block] yield link if block_given? raise AF83::Decorator::IncompleteLinkDefinition.new(link.errors) unless link.complete? @@ -16,6 +17,57 @@ module AF83::EnhancedDecorator @_action_links[weight] << link end + ### Here we define some shortcuts that match dthe default behaviours + def create_action_link args={}, &block + opts = { + on: :index, + primary: :index, + policy: :create, + before_block: -> (l){ + l.content { h.t('actions.add') } + l.href { [:new, object.klass.name.underscore.singularize] } + } + } + action_link opts.update(args), &block + end + + def show_action_link args={}, &block + opts = { + primary: :index, + before_block: -> (l){ + l.content { h.t('actions.show') } + l.href { [object] } + } + } + action_link opts.update(args), &block + end + + def edit_action_link args={}, &block + opts = { + primary: %i(show index), + before_block: -> (l){ + l.content { h.t('actions.edit') } + l.href { [object] } + } + } + action_link opts.update(args), &block + end + + def destroy_action_link args={}, &block + opts = { + policy: :destroy, + footer: true, + secondary: :show, + before_block: -> (l){ + l.content { h.destroy_link_content } + l.href { [object] } + l.method { :delete } + l.data {{ confirm: h.t('actions.destroy_confirm') }} + } + } + action_link opts.update(args), &block + end + def t key eval "-> (l){ h.t('#{key}') }" end @@ -32,7 +84,7 @@ module AF83::EnhancedDecorator def parse_options args options = {} - %i(weight primary secondary footer on action actions policy feature if groups group).each do |k| + %i(weight primary secondary footer on action actions policy feature if groups group before_block).each do |k| options[k] = args.delete(k) if args.has_key?(k) end link_options = args.dup |
