diff --git a/evil-client.gemspec b/evil-client.gemspec index f145554..c6ae382 100644 --- a/evil-client.gemspec +++ b/evil-client.gemspec @@ -16,7 +16,7 @@ Gem::Specification.new do |gem| gem.add_runtime_dependency "dry-initializer", "~> 2.1" gem.add_runtime_dependency "mime-types", "~> 3.1" gem.add_runtime_dependency "rack", "~> 2" - gem.add_runtime_dependency "tram-policy", "~> 0.2.2", "<= 0.2.3" + gem.add_runtime_dependency "tram-policy", "~> 0.2.3" gem.add_development_dependency "rake", ">= 10" gem.add_development_dependency "rspec", "~> 3.0" diff --git a/lib/evil/client/model.rb b/lib/evil/client/model.rb index 8f065d8..d69e656 100644 --- a/lib/evil/client/model.rb +++ b/lib/evil/client/model.rb @@ -79,22 +79,11 @@ def validate(&block) # @param [Evil::Client::Model] other # @return [self] # - # rubocop: disable Metrics/AbcSize def extend(other) return super if other.instance_of? Module - - unless other.ancestors.include? Evil::Client::Model - raise TypeError, "#{other} is not a subclass of Evil::Client::Model" - end - - other.dry_initializer.options.each do |definition| - option definition.source, definition.options - end - - other.lets.each { |key, block| let(key, &block) } - other.policy.all.each { |validator| policy.local << validator } + validate_model other + extend_model other end - # rubocop: enable Metrics/AbcSize # Model instance constructor # @@ -119,6 +108,23 @@ def in_english(&block) ensure I18n.available_locales = available_locales end + + def validate_model(other) + return if other.ancestors.include? Evil::Client::Model + raise TypeError, "#{other} is not a subclass of Evil::Client::Model" + end + + def extend_model(other) + other.dry_initializer.options.each do |definition| + option definition.source, definition.options + end + + other.lets.each { |key, block| let(key, &block) } + + other.policy.validators.each do |validator| + policy.local_validators << validator + end + end end end end diff --git a/lib/evil/client/policy.rb b/lib/evil/client/policy.rb index 7af0bb2..a9d63b1 100644 --- a/lib/evil/client/policy.rb +++ b/lib/evil/client/policy.rb @@ -32,11 +32,13 @@ def name alias_method :to_sym, :name alias_method :inspect, :name - private - + # Redefines translation scope for the policy + # + # @return[Array] + # def scope @scope ||= %i[evil client errors] << \ - Tram::Policy::Inflector.underscore(model.to_s) + Tram::Policy::Inflector.underscore(model.to_s).to_sym end end diff --git a/spec/fixtures/locales/en.yml b/spec/fixtures/locales/en.yml index 3740493..6cc3977 100644 --- a/spec/fixtures/locales/en.yml +++ b/spec/fixtures/locales/en.yml @@ -9,9 +9,12 @@ en: token_present: "To update user id:%{id} you must provide a token" name_present: "The user has no name" test/client: - valid_credentials: You should set either token or password to authenticate the client + valid_credentials: &vc You should set either token or password to authenticate the client crm: users: + valid_credentials: *vc + fetch: + valid_credentials: *vc filter: filter_given: You should define some filter with either name, email, or id test/model: