From 9c1f6d4a181d24cf532e09c984e2aa07519bbb64 Mon Sep 17 00:00:00 2001 From: Maurizio De Santis Date: Mon, 14 Jan 2019 18:19:21 +0100 Subject: [PATCH] PoC: Presenters --- .../spree/country_list_presenter.rb | 15 +++++++++++++ .../app/presenters/spree/country_presenter.rb | 21 +++++++++++++++++++ core/lib/spree/core.rb | 1 + core/lib/spree/presenter.rb | 7 +++++++ core/lib/spree/presenter/base.rb | 13 ++++++++++++ 5 files changed, 57 insertions(+) create mode 100644 core/app/presenters/spree/country_list_presenter.rb create mode 100644 core/app/presenters/spree/country_presenter.rb create mode 100644 core/lib/spree/presenter.rb create mode 100644 core/lib/spree/presenter/base.rb diff --git a/core/app/presenters/spree/country_list_presenter.rb b/core/app/presenters/spree/country_list_presenter.rb new file mode 100644 index 00000000000..4d94bbc254c --- /dev/null +++ b/core/app/presenters/spree/country_list_presenter.rb @@ -0,0 +1,15 @@ +module Spree + class CountryListPresenter < Spree::Presenter::Base + def initialize(presentee) + presentee = prepare_countries(presentee) + + super + end + + protected + + def prepare_countries(countries) + countries.map { |country| Spree::CountryPresenter.new(country) }.sort_by { |c| c.name.parameterize } + end + end +end diff --git a/core/app/presenters/spree/country_presenter.rb b/core/app/presenters/spree/country_presenter.rb new file mode 100644 index 00000000000..94249f7c31a --- /dev/null +++ b/core/app/presenters/spree/country_presenter.rb @@ -0,0 +1,21 @@ +module Spree + class CountryPresenter < Spree::Presenter::Base + def name + translated_name || carmen_name || super + end + + def carmen_country + Carmen::Country.coded iso + end + + def carmen_name + carmen_country.try :name + end + + def translated_name + return nil unless iso + + I18n.t("spree.country_names.#{iso}", default: nil) + end + end +end diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb index b0137099bfa..e19703a6b52 100644 --- a/core/lib/spree/core.rb +++ b/core/lib/spree/core.rb @@ -86,6 +86,7 @@ class GatewayError < RuntimeError; end require 'spree/core/stock_configuration' require 'spree/core/validators/email' require 'spree/permission_sets' +require 'spree/presenter' require 'spree/preferences/store' require 'spree/preferences/static_model_preferences' diff --git a/core/lib/spree/presenter.rb b/core/lib/spree/presenter.rb new file mode 100644 index 00000000000..3f8bea67aa0 --- /dev/null +++ b/core/lib/spree/presenter.rb @@ -0,0 +1,7 @@ +module Spree + module Presenter + end +end + +require 'spree/presenter/base' + diff --git a/core/lib/spree/presenter/base.rb b/core/lib/spree/presenter/base.rb new file mode 100644 index 00000000000..e23cadd48c9 --- /dev/null +++ b/core/lib/spree/presenter/base.rb @@ -0,0 +1,13 @@ +module Spree + module Presenter + class Base < SimpleDelegator + attr_reader :presentee + + def initialize(presentee) + @presentee = presentee + + super(presentee) + end + end + end +end