aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZog2018-01-16 16:51:58 +0100
committerZog2018-01-25 17:17:59 +0100
commit44d38ab6b6a05a56a73c443c62f4a88d1733f274 (patch)
tree6f93498e6d2316d27590803598dfea728caa459f
parent04e6d933e2c140dbdd24e29a6139d74fab3c386d (diff)
downloadchouette-core-44d38ab6b6a05a56a73c443c62f4a88d1733f274.tar.bz2
Refs #5586 @1h; Migrate TimeTableDecorator
-rw-r--r--app/controllers/time_tables_controller.rb3
-rw-r--r--app/decorators/line_decorator.rb1
-rw-r--r--app/decorators/time_table_decorator.rb71
-rw-r--r--app/views/time_tables/index.html.slim1
-rw-r--r--app/views/time_tables/show.html.slim15
-rw-r--r--lib/af83/decorator.rb8
-rw-r--r--lib/af83/enhanced_decorator.rb1
7 files changed, 46 insertions, 54 deletions
diff --git a/app/controllers/time_tables_controller.rb b/app/controllers/time_tables_controller.rb
index a0fa168f0..0707b9648 100644
--- a/app/controllers/time_tables_controller.rb
+++ b/app/controllers/time_tables_controller.rb
@@ -167,9 +167,8 @@ class TimeTablesController < ChouetteController
end
def decorate_time_tables(time_tables)
- ModelDecorator.decorate(
+ TimeTableDecorator.decorate(
time_tables,
- with: TimeTableDecorator,
context: {
referential: @referential
}
diff --git a/app/decorators/line_decorator.rb b/app/decorators/line_decorator.rb
index cc0e3a605..a8623e7d0 100644
--- a/app/decorators/line_decorator.rb
+++ b/app/decorators/line_decorator.rb
@@ -7,7 +7,6 @@ class LineDecorator < AF83::Decorator
end
with_instance_decorator do |instance_decorator|
- instance_decorator.delegate_all
### primary (and secondary) can be
### - a single action
### - an array of actions
diff --git a/app/decorators/time_table_decorator.rb b/app/decorators/time_table_decorator.rb
index c6eeac176..202a020f7 100644
--- a/app/decorators/time_table_decorator.rb
+++ b/app/decorators/time_table_decorator.rb
@@ -1,55 +1,58 @@
-class TimeTableDecorator < Draper::Decorator
+class TimeTableDecorator < AF83::Decorator
decorates Chouette::TimeTable
- delegate_all
+ with_instance_decorator do |instance_decorator|
+ instance_decorator.action_link primary: :index do |l|
+ l.content { h.t('actions.show') }
+ l.href { [context[:referential], object] }
+ end
- # Requires:
- # context: {
- # referential: ,
- # }
- def action_links
- links = []
+ instance_decorator.action_link primary: %i(show index) do |l|
+ l.content { h.t('actions.edit') }
+ l.href { [context[:referential], object] }
+ end
- if object.calendar
- links << Link.new(
- content: h.t('actions.actualize'),
- href: h.actualize_referential_time_table_path(
+ instance_decorator.action_link if: ->{ object.calendar }, secondary: true do |l|
+ l.content { h.t('actions.actualize') }
+ l.href do
+ h.actualize_referential_time_table_path(
context[:referential],
object
- ),
- method: :post
- )
+ )
+ end
+ l.method :post
end
- if h.policy(object).edit?
- links << Link.new(
- content: h.t('actions.combine'),
- href: h.new_referential_time_table_time_table_combination_path(
+ instance_decorator.action_link policy: :edit, secondary: true do |l|
+ l.content { h.t('actions.combine') }
+ l.href do
+ h.new_referential_time_table_time_table_combination_path(
context[:referential],
object
)
- )
+ end
end
- if h.policy(object).duplicate?
- links << Link.new(
- content: h.t('actions.duplicate'),
- href: h.duplicate_referential_time_table_path(
+ instance_decorator.action_link policy: :duplicate, secondary: true do |l|
+ l.content { h.t('actions.duplicate') }
+ l.href do
+ h.duplicate_referential_time_table_path(
context[:referential],
object
)
- )
+ end
end
- if h.policy(object).destroy?
- links << Link.new(
- content: h.destroy_link_content,
- href: h.referential_time_table_path(context[:referential], object),
- method: :delete,
- data: { confirm: h.t('time_tables.actions.destroy_confirm') }
- )
+ instance_decorator.action_link policy: :destroy, footer: true, secondary: :show do |l|
+ l.content { h.destroy_link_content }
+ 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
-
- links
end
end
diff --git a/app/views/time_tables/index.html.slim b/app/views/time_tables/index.html.slim
index b684b0bcb..b194dcea1 100644
--- a/app/views/time_tables/index.html.slim
+++ b/app/views/time_tables/index.html.slim
@@ -54,7 +54,6 @@
attribute: Proc.new { |tt| l(tt.updated_at, format: :short) } \
) \
],
- links: [:show, :edit],
cls: 'table has-search'
= new_pagination @time_tables, 'pull-right'
diff --git a/app/views/time_tables/show.html.slim b/app/views/time_tables/show.html.slim
index df9789055..6d15323f3 100644
--- a/app/views/time_tables/show.html.slim
+++ b/app/views/time_tables/show.html.slim
@@ -4,21 +4,6 @@
- content_for :page_header_title, t('time_tables.show.title', name: @time_table.comment), flush: true
-- content_for :page_header_actions do
- - if policy(@time_table).edit?
- = link_to(t('actions.edit'), edit_referential_time_table_path(@referential, @time_table), class: 'btn btn-default')
-
-- content_for :page_header_content do
- .row.mb-sm
- .col-lg-12.text-right
- - @time_table.action_links.each do |link|
- = link_to link.href,
- method: link.method,
- data: link.data,
- class: 'btn btn-primary' do
- = link.content
-
-
.page_content
.container-fluid
.row
diff --git a/lib/af83/decorator.rb b/lib/af83/decorator.rb
index ec62edaaa..82954adc1 100644
--- a/lib/af83/decorator.rb
+++ b/lib/af83/decorator.rb
@@ -7,11 +7,17 @@ class AF83::Decorator < ModelDecorator
end
def self.instance_decorator
- @instance_decorator ||= Class.new(AF83::Decorator::InstanceDecorator)
+ @instance_decorator ||= begin
+ klass = Class.new(AF83::Decorator::InstanceDecorator)
+ klass.delegate_all
+ klass
+ end
end
def self.with_instance_decorator
+ @_with_instance_decorator = true
yield instance_decorator
+ @_with_instance_decorator = false
end
def self.decorate object, options = {}
diff --git a/lib/af83/enhanced_decorator.rb b/lib/af83/enhanced_decorator.rb
index ad9a75000..91a377f8c 100644
--- a/lib/af83/enhanced_decorator.rb
+++ b/lib/af83/enhanced_decorator.rb
@@ -1,6 +1,7 @@
module AF83::EnhancedDecorator
module ClassMethods
def action_link args={}
+ raise "You are using `action_link` inside a with_instance_decorator block, but not on the instance decorator itself.\n Use `instance_decorator.action_link` or move outside of the block, as this may lead to an unforeseen behaviour." if @_with_instance_decorator
args[:if] = @_condition if args[:if].nil?
options, link_options = parse_options args