diff options
| author | Zog | 2018-01-17 09:36:15 +0100 | 
|---|---|---|
| committer | Zog | 2018-01-17 09:36:15 +0100 | 
| commit | 33b0cb432076fbb6e6904aeeff4e13c5003ed53f (patch) | |
| tree | e2677ff587cda0e19a644e2bc4b7e1c4c4f1881f | |
| parent | 421c5cf5156ef7830ed2164698035e5782d00837 (diff) | |
| download | chouette-core-33b0cb432076fbb6e6904aeeff4e13c5003ed53f.tar.bz2 | |
Refs #5586 @1.5h; Migrate more decorators
- Remove ApiKeyDecorator (never used)
- Migrate CalendarDecorator
- Migrate CompanyDecorator
- Migrate TimeTableDecorator
| -rw-r--r-- | app/controllers/api_keys_controller.rb | 7 | ||||
| -rw-r--r-- | app/controllers/application_controller.rb | 13 | ||||
| -rw-r--r-- | app/controllers/calendars_controller.rb | 7 | ||||
| -rw-r--r-- | app/controllers/companies_controller.rb | 6 | ||||
| -rw-r--r-- | app/controllers/lines_controller.rb | 4 | ||||
| -rw-r--r-- | app/controllers/referential_companies_controller.rb | 7 | ||||
| -rw-r--r-- | app/decorators/api_key_decorator.rb | 30 | ||||
| -rw-r--r-- | app/decorators/calendar_decorator.rb | 33 | ||||
| -rw-r--r-- | app/decorators/company_decorator.rb | 52 | ||||
| -rw-r--r-- | app/decorators/time_table_decorator.rb | 6 | ||||
| -rw-r--r-- | app/views/calendars/index.html.slim | 4 | ||||
| -rw-r--r-- | app/views/calendars/show.html.slim | 22 | ||||
| -rw-r--r-- | app/views/companies/index.html.slim | 4 | ||||
| -rw-r--r-- | app/views/companies/show.html.slim | 18 | ||||
| -rw-r--r-- | app/views/referential_companies/index.html.slim | 4 | ||||
| -rw-r--r-- | app/views/time_tables/index.html.slim | 3 | ||||
| -rw-r--r-- | lib/af83/decorator.rb | 3 | 
17 files changed, 83 insertions, 140 deletions
| diff --git a/app/controllers/api_keys_controller.rb b/app/controllers/api_keys_controller.rb index 9706c5961..a03a67481 100644 --- a/app/controllers/api_keys_controller.rb +++ b/app/controllers/api_keys_controller.rb @@ -31,11 +31,4 @@ class ApiKeysController < ChouetteController    def api_key_params      params.require(:api_key).permit(:name, :referential_id)    end - -  def decorate_api_keys(api_keys) -    ModelDecorator.decorate( -      api_keys, -      with: ApiKeyDecorator, -    ) -  end  end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 80d194096..8bd3da2f9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -52,6 +52,19 @@ class ApplicationController < ActionController::Base    end    helper_method :current_functional_scope +  def collection_name +    self.class.name.split("::").last.gsub('Controller', '').underscore +  end + +  def decorated_collection +    if instance_variable_defined?("@#{collection_name}") +      instance_variable_get("@#{collection_name}") +    else +      nil +    end +  end +  helper_method :decorated_collection +    def begin_of_association_chain      current_organisation    end diff --git a/app/controllers/calendars_controller.rb b/app/controllers/calendars_controller.rb index 4a752f2b9..de8f9d10c 100644 --- a/app/controllers/calendars_controller.rb +++ b/app/controllers/calendars_controller.rb @@ -7,14 +7,11 @@ class CalendarsController < ChouetteController    def index      index! do -      @calendars = ModelDecorator.decorate(@calendars, with: CalendarDecorator) +      @calendars = CalendarDecorator.decorate(@calendars)      end    end    def show -    show! do -      @calendar = @calendar.decorate -    end    end    private @@ -34,7 +31,7 @@ class CalendarsController < ChouetteController    protected    def resource -    @calendar = Calendar.where('organisation_id = ? OR shared = true', current_organisation.id).find_by_id(params[:id]) +    @calendar = Calendar.where('organisation_id = ? OR shared = true', current_organisation.id).find_by_id(params[:id]).decorate    end    def build_resource diff --git a/app/controllers/companies_controller.rb b/app/controllers/companies_controller.rb index f84252920..4afd12be1 100644 --- a/app/controllers/companies_controller.rb +++ b/app/controllers/companies_controller.rb @@ -47,6 +47,9 @@ class CompaniesController < ChouetteController      end    end +  def resource +    super.decorate(context: { referential: line_referential }) +  end    def resource_url(company = nil)      line_referential_company_path(line_referential, company || resource) @@ -79,9 +82,8 @@ class CompaniesController < ChouetteController    end    def decorate_companies(companies) -    ModelDecorator.decorate( +    CompanyDecorator.decorate(        companies, -      with: CompanyDecorator,        context: {          referential: line_referential        } diff --git a/app/controllers/lines_controller.rb b/app/controllers/lines_controller.rb index 10ecc7e9d..22578187f 100644 --- a/app/controllers/lines_controller.rb +++ b/app/controllers/lines_controller.rb @@ -70,10 +70,6 @@ class LinesController < ChouetteController      end    end -  def decorated_collection -    @lines -  end -    helper_method :decorated_collection    protected diff --git a/app/controllers/referential_companies_controller.rb b/app/controllers/referential_companies_controller.rb index 7e65a72cf..806a70c8f 100644 --- a/app/controllers/referential_companies_controller.rb +++ b/app/controllers/referential_companies_controller.rb @@ -69,10 +69,13 @@ class ReferentialCompaniesController < ChouetteController      %w[asc desc].include?(params[:direction]) ?  params[:direction] : 'asc'    end +  def collection_name +    "companies" +  end +    def decorate_companies(companies) -    ModelDecorator.decorate( +    CompanyDecorator.decorate(        companies, -      with: CompanyDecorator,        context: {          referential: referential        } diff --git a/app/decorators/api_key_decorator.rb b/app/decorators/api_key_decorator.rb deleted file mode 100644 index def3a6a01..000000000 --- a/app/decorators/api_key_decorator.rb +++ /dev/null @@ -1,30 +0,0 @@ -class ApiKeyDecorator < Draper::Decorator -  decorates Api::V1::ApiKey -  delegate_all - - -  def action_links -    links = [] - -    links << Link.new( -      content: h.t('api_keys.actions.show'), -      href: h.organisation_api_key_path(object), -    ) - -    links << Link.new( -      content: h.t('api_keys.actions.edit'), -      href: h.edit_organisation_api_key_path(object), -    ) - -    if h.policy(object).destroy? -      links << Link.new( -        content: h.destroy_link_content, -        href: h.organisation_api_key_path(object), -        method: :delete, -        data: { confirm: h.t('api_keys.actions.destroy_confirm') } -      ) -    end - -    links -  end -end diff --git a/app/decorators/calendar_decorator.rb b/app/decorators/calendar_decorator.rb index 37e2cfe80..c47e4d1d5 100644 --- a/app/decorators/calendar_decorator.rb +++ b/app/decorators/calendar_decorator.rb @@ -1,18 +1,27 @@ -class CalendarDecorator < Draper::Decorator -  delegate_all +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 -  def action_links -    links = [] +  with_instance_decorator do |instance_decorator| +    instance_decorator.action_link primary: :index do |l| +      l.content { h.t('actions.show') } +      l.href { [object] } +    end -    if h.policy(object).destroy? -      links << Link.new( -        content: h.destroy_link_content, -        href: h.calendar_path(object), -        method: :delete, -        data: { confirm: h.t('calendars.actions.destroy_confirm') } -      ) +    instance_decorator.action_link primary: %i(show index) do |l| +      l.content { h.t('actions.edit') } +      l.href { [object] }      end -    links +    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 } +      l.data {{ confirm: h.t('calendars.actions.destroy_confirm') }} +    end    end  end diff --git a/app/decorators/company_decorator.rb b/app/decorators/company_decorator.rb index 50b82d276..631e030db 100644 --- a/app/decorators/company_decorator.rb +++ b/app/decorators/company_decorator.rb @@ -1,45 +1,37 @@ -class CompanyDecorator < Draper::Decorator +class CompanyDecorator < AF83::Decorator    decorates Chouette::Company -  delegate_all - -  def self.collection_decorator_class -    PaginatingDecorator -  end - -  def linecount -    object.lines.count +  action_link on: :index, primary: :index, policy: :create do |l| +    l.content { h.t('companies.actions.new') } +    l.href    { [:new, context[:referential], :company] }    end -  # Requires: -  #   context: { -  #     referential: -  #   } -  def action_links -    links = [] +  with_instance_decorator do |instance_decorator| +    instance_decorator.action_link primary: :index do |l| +      l.content { h.t('actions.show') } +      l.href { [object] } +    end -    if h.policy(object).update? -      links << Link.new( -        content: h.t('companies.actions.edit'), -        href: h.edit_line_referential_company_path( +    instance_decorator.action_link primary: %i(show index) 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(            context[:referential],            object          ) -      ) +      }      end -    if h.policy(object).destroy? -      links << Link.new( -        content: h.destroy_link_content('companies.actions.destroy'), -        href: h.line_referential_company_path( +    instance_decorator.action_link policy: :destroy, footer: true, secondary: :show  do |l| +      l.content { h.destroy_link_content('companies.actions.destroy') } +      l.href { +        h.edit_line_referential_company_path(            context[:referential],            object -        ), -        method: :delete, -        data: { confirm: h.t('companies.actions.destroy_confirm') } -      ) +        ) +      } +      l.method { :delete } +      l.data {{ confirm: h.t('companies.actions.destroy_confirm') }}      end - -    links    end  end diff --git a/app/decorators/time_table_decorator.rb b/app/decorators/time_table_decorator.rb index 202a020f7..fc808f091 100644 --- a/app/decorators/time_table_decorator.rb +++ b/app/decorators/time_table_decorator.rb @@ -1,6 +1,12 @@  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]) } +  end +    with_instance_decorator do |instance_decorator|      instance_decorator.action_link primary: :index do |l|        l.content { h.t('actions.show') } diff --git a/app/views/calendars/index.html.slim b/app/views/calendars/index.html.slim index 77478a624..92c24be5b 100644 --- a/app/views/calendars/index.html.slim +++ b/app/views/calendars/index.html.slim @@ -1,7 +1,4 @@  - breadcrumb :calendars -- content_for :page_header_actions do -  - if policy(Calendar).create? -    = link_to(t('actions.add'), new_calendar_path, class: 'btn btn-default')  .page_content    .container-fluid @@ -35,7 +32,6 @@                  attribute: Proc.new { |c| t("#{c.try(:shared)}") } \                ) \              ], -            links: [:show, :edit],              cls: 'table has-filter'            = new_pagination @calendars, 'pull-right' diff --git a/app/views/calendars/show.html.slim b/app/views/calendars/show.html.slim index da4afa3e6..8eb66dc60 100644 --- a/app/views/calendars/show.html.slim +++ b/app/views/calendars/show.html.slim @@ -1,25 +1,13 @@  - breadcrumb :calendar, @calendar  - page_header_content_for @calendar -- content_for :page_header_content do -  .row.mb-sm -    .col-lg-12.text-right -      - @calendar.action_links.each do |link| -        = link_to link.href, -            method: link.method, -            data: link.data, -            class: 'btn btn-primary' do -              = link.content -- if policy(@calendar).edit? -  - content_for :page_header_actions do -      = link_to(t('actions.edit'), edit_calendar_path(@calendar), class: 'btn btn-default')  .page_content    .container-fluid      .row        .col-lg-6.col-md-6.col-sm-12.col-xs-12          = definition_list t('metadatas'), -          { 'Nom court' => @calendar.try(:short_name), -            Calendar.human_attribute_name(:shared) => t("#{@calendar.shared}"), -            'Organisation' => @calendar.organisation.name, -            Calendar.human_attribute_name(:dates) =>  @calendar.dates.collect{|d| l(d, format: :short)}.join(', ').html_safe, -            Calendar.human_attribute_name(:date_ranges) => @calendar.periods.map{|d| t('validity_range', debut: l(d.begin, format: :short), end: l(d.end, format: :short))}.join('<br>').html_safe } +          { 'Nom court' => resource.try(:short_name), +            Calendar.human_attribute_name(:shared) => t("#{resource.shared}"), +            'Organisation' => resource.organisation.name, +            Calendar.human_attribute_name(:dates) =>  resource.dates.collect{|d| l(d, format: :short)}.join(', ').html_safe, +            Calendar.human_attribute_name(:date_ranges) => resource.periods.map{|d| t('validity_range', debut: l(d.begin, format: :short), end: l(d.end, format: :short))}.join('<br>').html_safe } diff --git a/app/views/companies/index.html.slim b/app/views/companies/index.html.slim index e031f3776..9f1502e54 100644 --- a/app/views/companies/index.html.slim +++ b/app/views/companies/index.html.slim @@ -1,7 +1,4 @@  - breadcrumb :companies, @line_referential -- content_for :page_header_actions do -  - if policy(Chouette::Company).create? -    = link_to(t('companies.actions.new'), new_line_referential_company_path(@line_referential), class: 'btn btn-primary')  .page_content    .container-fluid @@ -34,7 +31,6 @@                  end \                ) \              ], -            links: [:show],              cls: 'table has-search'            = new_pagination @companies, 'pull-right' diff --git a/app/views/companies/show.html.slim b/app/views/companies/show.html.slim index 0d6b4aae3..ca0a410b3 100644 --- a/app/views/companies/show.html.slim +++ b/app/views/companies/show.html.slim @@ -1,25 +1,13 @@  - breadcrumb :company, @company -- content_for :page_header_content do -  .row -    .col-lg-12.text-right.mb-sm -      - if policy(Chouette::Company).create? -        = link_to t('companies.actions.new'), new_line_referential_company_path(@line_referential), class: 'btn btn-primary' -      - if policy(@company).update? -        = link_to t('companies.actions.edit'), edit_line_referential_company_path(@line_referential, @company), class: 'btn btn-primary' -      - if policy(@company).destroy? -        = link_to line_referential_company_path(@line_referential, @company), method: :delete, data: {confirm:  t('companies.actions.destroy_confirm')}, class: 'btn btn-primary' do -          span.fa.fa-trash -          span = t('companies.actions.destroy')  - page_header_content_for @company -  .page_content    .container-fluid      .row        .col-lg-6.col-md-6.col-sm-12.col-xs-12          = definition_list t('metadatas'),            { 'ID Codif' => @company.try(:get_objectid).try(:short_id), -            Chouette::Company.human_attribute_name(:phone) => @company.phone, -            Chouette::Company.human_attribute_name(:email) => @company.email, -            Chouette::Company.human_attribute_name(:url) => @company.url } +            Chouette::Company.human_attribute_name(:phone) => resource.phone, +            Chouette::Company.human_attribute_name(:email) => resource.email, +            Chouette::Company.human_attribute_name(:url) => resource.url } diff --git a/app/views/referential_companies/index.html.slim b/app/views/referential_companies/index.html.slim index 07de2bc9d..3bff448c7 100644 --- a/app/views/referential_companies/index.html.slim +++ b/app/views/referential_companies/index.html.slim @@ -1,7 +1,4 @@  - breadcrumb :referential_companies, @referential -- content_for :page_header_actions do -  - if policy(Chouette::Company).create? -    = link_to(t('companies.actions.new'), new_referential_company_path(@referential), class: 'btn btn-default')  .page_content    .container-fluid @@ -46,7 +43,6 @@                  attribute: 'url' \                ) \              ], -            links: [:show],              cls: 'table has-search'            = new_pagination @companies, 'pull-right' diff --git a/app/views/time_tables/index.html.slim b/app/views/time_tables/index.html.slim index b194dcea1..f58fbb5ea 100644 --- a/app/views/time_tables/index.html.slim +++ b/app/views/time_tables/index.html.slim @@ -1,7 +1,4 @@  - breadcrumb :time_tables, @referential -- content_for :page_header_actions do -  - if (policy(Chouette::TimeTable).create? && @referential.organisation == current_organisation) -    = link_to(t('actions.add'), new_referential_time_table_path(@referential), class: 'btn btn-default')  .page_content    .container-fluid diff --git a/lib/af83/decorator.rb b/lib/af83/decorator.rb index 82954adc1..80958fa02 100644 --- a/lib/af83/decorator.rb +++ b/lib/af83/decorator.rb @@ -111,6 +111,7 @@ class AF83::Decorator < ModelDecorator      REQUIRED_ATTRIBUTES = %i(href content)      attr_reader :context +    attr_reader :action      def initialize options={}        @options = options @@ -233,7 +234,7 @@ class AF83::Decorator < ModelDecorator        if block_given?          link = Link.new(@options)          yield link -        return link.bind_to_context(context, @options[:action]).to_html +        return link.bind_to_context(context, @action).to_html        end        context.h.link_to content, href, html_options      end | 
