Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC: Presenters [WIP] #21

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/app/helpers/spree/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ def available_countries(restrict_to_zone: Spree::Config[:checkout_zone])
countries = Country.all
end

Spree::CountryListPresenter.new(countries)
end

def countries_for_collection_select(countries)
country_names = Carmen::Country.all.map do |country|
[country.code, country.name]
end.to_h
Expand Down
15 changes: 15 additions & 0 deletions core/app/presenters/spree/country_list_presenter.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions core/app/presenters/spree/country_presenter.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions core/lib/spree/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
7 changes: 7 additions & 0 deletions core/lib/spree/presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Spree
module Presenter
end
end

require 'spree/presenter/base'

13 changes: 13 additions & 0 deletions core/lib/spree/presenter/base.rb
Original file line number Diff line number Diff line change
@@ -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