-
-
Notifications
You must be signed in to change notification settings - Fork 913
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
Scope parameters according to Rails version #989
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,79 +2,130 @@ module Shoulda | |
module Matchers | ||
# @private | ||
class RailsShim | ||
def self.verb_for_update | ||
if action_pack_gte_4_1? | ||
:patch | ||
else | ||
:put | ||
class << self | ||
def action_pack_gte_4_1? | ||
Gem::Requirement.new('>= 4.1').satisfied_by?(action_pack_version) | ||
end | ||
end | ||
|
||
def self.type_cast_default_for(model, column) | ||
if model.respond_to?(:column_defaults) | ||
# Rails 4.2 | ||
model.column_defaults[column.name] | ||
else | ||
column.default | ||
def action_pack_gte_5? | ||
Gem::Requirement.new('>= 5').satisfied_by?(action_pack_version) | ||
end | ||
|
||
def action_pack_version | ||
Gem::Version.new(::ActionPack::VERSION::STRING) | ||
end | ||
|
||
def active_record_major_version | ||
::ActiveRecord::VERSION::MAJOR | ||
end | ||
end | ||
|
||
def self.serialized_attributes_for(model) | ||
if defined?(::ActiveRecord::Type::Serialized) | ||
# Rails 5+ | ||
model.columns.select do |column| | ||
model.type_for_attribute(column.name).is_a?( | ||
::ActiveRecord::Type::Serialized, | ||
def generate_validation_message( | ||
record, | ||
attribute, | ||
type, | ||
model_name, | ||
options | ||
) | ||
if record && record.errors.respond_to?(:generate_message) | ||
record.errors.generate_message(attribute.to_sym, type, options) | ||
else | ||
simply_generate_validation_message( | ||
attribute, | ||
type, | ||
model_name, | ||
options, | ||
) | ||
end.inject({}) do |hash, column| | ||
hash[column.name.to_s] = model.type_for_attribute(column.name).coder | ||
hash | ||
end | ||
else | ||
model.serialized_attributes | ||
rescue RangeError | ||
simply_generate_validation_message( | ||
attribute, | ||
type, | ||
model_name, | ||
options, | ||
) | ||
end | ||
end | ||
|
||
def self.generate_validation_message(record, attribute, type, model_name, options) | ||
if record && record.errors.respond_to?(:generate_message) | ||
record.errors.generate_message(attribute.to_sym, type, options) | ||
else | ||
simply_generate_validation_message(attribute, type, model_name, options) | ||
def make_controller_request(context, verb, action, request_params) | ||
params = | ||
if action_pack_gte_5? | ||
{ params: request_params } | ||
else | ||
request_params | ||
end | ||
|
||
context.__send__(verb, action, params) | ||
end | ||
rescue RangeError | ||
simply_generate_validation_message(attribute, type, model_name, options) | ||
end | ||
|
||
def self.simply_generate_validation_message(attribute, type, model_name, options) | ||
default_translation_keys = [ | ||
:"activerecord.errors.models.#{model_name}.#{type}", | ||
:"activerecord.errors.messages.#{type}", | ||
:"errors.attributes.#{attribute}.#{type}", | ||
:"errors.messages.#{type}" | ||
] | ||
primary_translation_key = :"activerecord.errors.models.#{model_name}.attributes.#{attribute}.#{type}" | ||
translate_options = { default: default_translation_keys }.merge(options) | ||
I18n.translate(primary_translation_key, translate_options) | ||
end | ||
def serialized_attributes_for(model) | ||
if defined?(::ActiveRecord::Type::Serialized) | ||
# Rails 5+ | ||
serialized_columns = model.columns.select do |column| | ||
model.type_for_attribute(column.name).is_a?( | ||
::ActiveRecord::Type::Serialized, | ||
) | ||
end | ||
|
||
def self.tables_and_views(connection) | ||
if active_record_major_version >= 5 | ||
connection.data_sources | ||
else | ||
connection.tables | ||
serialized_columns.inject({}) do |hash, column| | ||
hash[column.name.to_s] = model.type_for_attribute(column.name).coder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [82/80] |
||
hash | ||
end | ||
else | ||
model.serialized_attributes | ||
end | ||
end | ||
end | ||
|
||
def self.active_record_major_version | ||
::ActiveRecord::VERSION::MAJOR | ||
end | ||
def type_cast_default_for(model, column) | ||
if model.respond_to?(:column_defaults) | ||
# Rails 4.2 | ||
model.column_defaults[column.name] | ||
else | ||
column.default | ||
end | ||
end | ||
|
||
def self.action_pack_gte_4_1? | ||
Gem::Requirement.new('>= 4.1').satisfied_by?(action_pack_version) | ||
end | ||
def tables_and_views(connection) | ||
if active_record_major_version >= 5 | ||
connection.data_sources | ||
else | ||
connection.tables | ||
end | ||
end | ||
|
||
def self.action_pack_version | ||
Gem::Version.new(::ActionPack::VERSION::STRING) | ||
def verb_for_update | ||
if action_pack_gte_4_1? | ||
:patch | ||
else | ||
:put | ||
end | ||
end | ||
|
||
private | ||
|
||
def simply_generate_validation_message( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method has too many lines. [18/10] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method has too many lines. [18/10] |
||
attribute, | ||
type, | ||
model_name, | ||
options | ||
) | ||
default_translation_keys = [ | ||
:"activerecord.errors.models.#{model_name}.#{type}", | ||
:"activerecord.errors.messages.#{type}", | ||
:"errors.attributes.#{attribute}.#{type}", | ||
:"errors.messages.#{type}", | ||
] | ||
primary_translation_key = [ | ||
:activerecord, | ||
:errors, | ||
:models, | ||
model_name, | ||
:attributes, | ||
attribute, | ||
type, | ||
] | ||
translate_options = | ||
{ default: default_translation_keys }.merge(options) | ||
I18n.translate(primary_translation_key, translate_options) | ||
end | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module UnitTests | ||
module ActionPackVersions | ||
extend self | ||
|
||
def self.configure_example_group(example_group) | ||
example_group.include(self) | ||
example_group.extend(self) | ||
end | ||
|
||
def action_pack_gte_5? | ||
action_pack_version =~ '>= 5' | ||
end | ||
|
||
def action_pack_version | ||
Tests::Version.new(ActionPack::VERSION::STRING) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Method has too many lines. [14/10]