Skip to content

Commit

Permalink
PoC: Presenters
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesantis committed Jan 15, 2019
1 parent e3ae5c9 commit 9c1f6d4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
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

0 comments on commit 9c1f6d4

Please sign in to comment.