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

Override model_name.human for PaymentMethod #2107

Merged
merged 1 commit into from
Jul 25, 2017
Merged
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
19 changes: 19 additions & 0 deletions core/app/models/spree/payment_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ class PaymentMethod < Spree::Base

include Spree::Preferences::StaticallyConfigurable

# Custom ModelName#human implementation to ensure we don't refer to
# subclasses as just "PaymentMethod"
class ModelName < ActiveModel::Name
# Similar to ActiveModel::Name#human, but skips lookup_ancestors
def human(options = {})
defaults = [
i18n_key,
options[:default],
@human
].compact
options = { scope: [:activerecord, :models], count: 1, default: defaults }.merge!(options.except(:default))
I18n.translate(defaults.shift, options)
end
end

class << self
def providers
Spree::Deprecation.warn 'Spree::PaymentMethod.providers is deprecated and will be deleted in Solidus 3.0. ' \
Expand Down Expand Up @@ -66,6 +81,10 @@ def available(display_on = nil, store: nil)
end
end

def model_name
ModelName.new(self, Spree)
end

def active?
where(type: to_s, active: true).count > 0
end
Expand Down
16 changes: 16 additions & 0 deletions core/spec/models/spree/payment_method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,20 @@ def gateway_class
expect(payment_method.credit).to eq 'credit'
end
end

describe 'model_name.human' do
context 'PaymentMethod itself' do
it "returns i18n value" do
expect(Spree::PaymentMethod.model_name.human).to eq('Payment Method')
end
end

context 'A subclass with no i18n key' do
let!(:klass) { stub_const("MyGem::SomeClass", Class.new(described_class)) }

it "returns default humanized value" do
expect(klass.model_name.human).to eq('Some class')
end
end
end
end