Skip to content

Commit

Permalink
Convert ActiveModel to 1.9 hash syntax.
Browse files Browse the repository at this point in the history
I also attempted to fix other styleguide violations such as
{ a: :b } over {a: :b} and foo(b: 'bar') over foo( b: 'bar' ).
  • Loading branch information
patricksrobertson committed May 2, 2013
1 parent dd1f360 commit eebb9dd
Show file tree
Hide file tree
Showing 43 changed files with 351 additions and 349 deletions.
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/attribute_methods.rb
Expand Up @@ -343,7 +343,7 @@ def instance_method_already_implemented?(method_name) #:nodoc:
# significantly (in our case our test suite finishes 10% faster with
# this cache).
def attribute_method_matchers_cache #:nodoc:
@attribute_method_matchers_cache ||= ThreadSafe::Cache.new(:initial_capacity => 4)
@attribute_method_matchers_cache ||= ThreadSafe::Cache.new(initial_capacity: 4)
end

def attribute_method_matcher(method_name) #:nodoc:
Expand Down
8 changes: 4 additions & 4 deletions activemodel/lib/active_model/callbacks.rb
Expand Up @@ -100,10 +100,10 @@ def self.extended(base) #:nodoc:
def define_model_callbacks(*callbacks)
options = callbacks.extract_options!
options = {
:terminator => "result == false",
:skip_after_callbacks_if_terminated => true,
:scope => [:kind, :name],
:only => [:before, :around, :after]
terminator: "result == false",
skip_after_callbacks_if_terminated: true,
scope: [:kind, :name],
only: [:before, :around, :after]
}.merge!(options)

types = Array(options.delete(:only))
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/dirty.rb
Expand Up @@ -91,7 +91,7 @@ module Dirty

included do
attribute_method_suffix '_changed?', '_change', '_will_change!', '_was'
attribute_method_affix :prefix => 'reset_', :suffix => '!'
attribute_method_affix prefix: 'reset_', suffix: '!'
end

# Returns +true+ if any attribute have unsaved changes, +false+ otherwise.
Expand Down
18 changes: 9 additions & 9 deletions activemodel/lib/active_model/errors.rb
Expand Up @@ -231,7 +231,7 @@ def empty?
# # <error>name must be specified</error>
# # </errors>
def to_xml(options={})
to_a.to_xml({ :root => "errors", :skip_types => true }.merge!(options))
to_a.to_xml({ root: "errors", skip_types: true }.merge!(options))
end

# Returns a Hash that can be used as the JSON representation for this
Expand Down Expand Up @@ -370,11 +370,11 @@ def full_messages_for(attribute)
def full_message(attribute, message)
return message if attribute == :base
attr_name = attribute.to_s.tr('.', '_').humanize
attr_name = @base.class.human_attribute_name(attribute, :default => attr_name)
attr_name = @base.class.human_attribute_name(attribute, default: attr_name)
I18n.t(:"errors.format", {
:default => "%{attribute} %{message}",
:attribute => attr_name,
:message => message
default: "%{attribute} %{message}",
attribute: attr_name,
message: message
})
end

Expand Down Expand Up @@ -426,10 +426,10 @@ def generate_message(attribute, type = :invalid, options = {})
value = (attribute != :base ? @base.send(:read_attribute_for_validation, attribute) : nil)

options = {
:default => defaults,
:model => @base.class.model_name.human,
:attribute => @base.class.human_attribute_name(attribute),
:value => value
default: defaults,
model: @base.class.model_name.human,
attribute: @base.class.human_attribute_name(attribute),
value: value
}.merge!(options)

I18n.translate(key, options)
Expand Down
4 changes: 2 additions & 2 deletions activemodel/lib/active_model/naming.rb
Expand Up @@ -129,7 +129,7 @@ class Name
#
# Equivalent to +to_s+.
delegate :==, :===, :<=>, :=~, :"!~", :eql?, :to_s,
:to_str, :to => :name
:to_str, to: :name

# Returns a new ActiveModel::Name instance. By default, the +namespace+
# and +name+ option will take the namespace and name of the given class
Expand Down Expand Up @@ -183,7 +183,7 @@ def human(options={})
defaults << options[:default] if options[:default]
defaults << @human

options = { :scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults }.merge!(options.except(:default))
options = { scope: [@klass.i18n_scope, :models], count: 1, default: defaults }.merge!(options.except(:default))
I18n.translate(defaults.shift, options)
end

Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/secure_password.rb
Expand Up @@ -57,7 +57,7 @@ def has_secure_password(options = {})

if options.fetch(:validations, true)
validates_confirmation_of :password
validates_presence_of :password, :on => :create
validates_presence_of :password, on: :create

before_create { raise "Password digest missing on new record" if password_digest.blank? }
end
Expand Down
10 changes: 5 additions & 5 deletions activemodel/lib/active_model/serializers/xml.rb
Expand Up @@ -79,7 +79,7 @@ def serialize
require 'builder' unless defined? ::Builder

options[:indent] ||= 2
options[:builder] ||= ::Builder::XmlMarkup.new(:indent => options[:indent])
options[:builder] ||= ::Builder::XmlMarkup.new(indent: options[:indent])

@builder = options[:builder]
@builder.instruct! unless options[:skip_instruct]
Expand All @@ -88,8 +88,8 @@ def serialize
root = ActiveSupport::XmlMini.rename_key(root, options)

args = [root]
args << {:xmlns => options[:namespace]} if options[:namespace]
args << {:type => options[:type]} if options[:type] && !options[:skip_types]
args << { xmlns: options[:namespace] } if options[:namespace]
args << { type: options[:type] } if options[:type] && !options[:skip_types]

@builder.tag!(*args) do
add_attributes_and_methods
Expand Down Expand Up @@ -132,7 +132,7 @@ def add_associations(association, records, opts)
records = records.to_ary

tag = ActiveSupport::XmlMini.rename_key(association.to_s, options)
type = options[:skip_types] ? { } : {:type => "array"}
type = options[:skip_types] ? { } : { type: "array" }
association_name = association.to_s.singularize
merged_options[:root] = association_name

Expand All @@ -145,7 +145,7 @@ def add_associations(association, records, opts)
record_type = {}
else
record_class = (record.class.to_s.underscore == association_name) ? nil : record.class.name
record_type = {:type => record_class}
record_type = { type: record_class }
end

record.to_xml merged_options.merge(record_type)
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/translation.rb
Expand Up @@ -41,7 +41,7 @@ def lookup_ancestors
#
# Specify +options+ with additional translating options.
def human_attribute_name(attribute, options = {})
options = { :count => 1 }.merge!(options)
options = { count: 1 }.merge!(options)
parts = attribute.to_s.split(".")
attribute = parts.pop
namespace = parts.join("/") unless parts.empty?
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/validations.rb
Expand Up @@ -46,7 +46,7 @@ module Validations
include HelperMethods

attr_accessor :validation_context
define_callbacks :validate, :scope => :name
define_callbacks :validate, scope: :name

class_attribute :_validators
self._validators = Hash.new { |h,k| h[k] = [] }
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/validations/acceptance.rb
Expand Up @@ -3,7 +3,7 @@ module ActiveModel
module Validations
class AcceptanceValidator < EachValidator # :nodoc:
def initialize(options)
super({ :allow_nil => true, :accept => "1" }.merge!(options))
super({ allow_nil: true, accept: "1" }.merge!(options))
end

def validate_each(record, attribute, value)
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/validations/callbacks.rb
Expand Up @@ -22,7 +22,7 @@ module Callbacks

included do
include ActiveSupport::Callbacks
define_callbacks :validation, :terminator => "result == false", :skip_after_callbacks_if_terminated => true, :scope => [:kind, :name]
define_callbacks :validation, terminator: "result == false", skip_after_callbacks_if_terminated: true, scope: [:kind, :name]
end

module ClassMethods
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/validations/confirmation.rb
Expand Up @@ -5,7 +5,7 @@ class ConfirmationValidator < EachValidator # :nodoc:
def validate_each(record, attribute, value)
if (confirmed = record.send("#{attribute}_confirmation")) && (value != confirmed)
human_attribute_name = record.class.human_attribute_name(attribute)
record.errors.add(:"#{attribute}_confirmation", :confirmation, options.merge(:attribute => human_attribute_name))
record.errors.add(:"#{attribute}_confirmation", :confirmation, options.merge(attribute: human_attribute_name))
end
end

Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/validations/exclusion.rb
Expand Up @@ -8,7 +8,7 @@ class ExclusionValidator < EachValidator # :nodoc:

def validate_each(record, attribute, value)
if include?(record, value)
record.errors.add(attribute, :exclusion, options.except(:in, :within).merge!(:value => value))
record.errors.add(attribute, :exclusion, options.except(:in, :within).merge!(value: value))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/validations/format.rb
Expand Up @@ -29,7 +29,7 @@ def option_call(record, name)
end

def record_error(record, attribute, name, value)
record.errors.add(attribute, :invalid, options.except(name).merge!(:value => value))
record.errors.add(attribute, :invalid, options.except(name).merge!(value: value))
end

def regexp_using_multiline_anchors?(regexp)
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/validations/inclusion.rb
Expand Up @@ -8,7 +8,7 @@ class InclusionValidator < EachValidator # :nodoc:

def validate_each(record, attribute, value)
unless include?(record, value)
record.errors.add(attribute, :inclusion, options.except(:in, :within).merge!(:value => value))
record.errors.add(attribute, :inclusion, options.except(:in, :within).merge!(value: value))
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions activemodel/lib/active_model/validations/length.rb
Expand Up @@ -3,8 +3,8 @@ module ActiveModel
# == Active \Model Length \Validator
module Validations
class LengthValidator < EachValidator # :nodoc:
MESSAGES = { :is => :wrong_length, :minimum => :too_short, :maximum => :too_long }.freeze
CHECKS = { :is => :==, :minimum => :>=, :maximum => :<= }.freeze
MESSAGES = { is: :wrong_length, minimum: :too_short, maximum: :too_long }.freeze
CHECKS = { is: :==, minimum: :>=, maximum: :<= }.freeze

RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :tokenizer, :too_short, :too_long]

Expand Down
10 changes: 5 additions & 5 deletions activemodel/lib/active_model/validations/numericality.rb
Expand Up @@ -2,9 +2,9 @@ module ActiveModel

module Validations
class NumericalityValidator < EachValidator # :nodoc:
CHECKS = { :greater_than => :>, :greater_than_or_equal_to => :>=,
:equal_to => :==, :less_than => :<, :less_than_or_equal_to => :<=,
:odd => :odd?, :even => :even?, :other_than => :!= }.freeze
CHECKS = { greater_than: :>, greater_than_or_equal_to: :>=,
equal_to: :==, less_than: :<, less_than_or_equal_to: :<=,
odd: :odd?, even: :even?, other_than: :!= }.freeze

RESERVED_OPTIONS = CHECKS.keys + [:only_integer]

Expand Down Expand Up @@ -47,7 +47,7 @@ def validate_each(record, attr_name, value)
option_value = record.send(option_value) if option_value.is_a?(Symbol)

unless value.send(CHECKS[option], option_value)
record.errors.add(attr_name, option, filtered_options(value).merge(:count => option_value))
record.errors.add(attr_name, option, filtered_options(value).merge(count: option_value))
end
end
end
Expand All @@ -73,7 +73,7 @@ def parse_raw_value_as_an_integer(raw_value)
end

def filtered_options(value)
options.except(*RESERVED_OPTIONS).merge!(:value => value)
options.except(*RESERVED_OPTIONS).merge!(value: value)
end
end

Expand Down
4 changes: 2 additions & 2 deletions activemodel/lib/active_model/validations/validates.rb
Expand Up @@ -159,9 +159,9 @@ def _parse_validates_options(options) # :nodoc:
when Hash
options
when Range, Array
{ :in => options }
{ in: options }
else
{ :with => options }
{ with: options }
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions activemodel/test/cases/attribute_methods_test.rb
Expand Up @@ -10,7 +10,7 @@ class << self
end

def attributes
{ :foo => 'value of foo', :baz => 'value of baz' }
{ foo: 'value of foo', baz: 'value of baz' }
end

private
Expand Down Expand Up @@ -80,7 +80,7 @@ class ModelWithRubyKeywordNamedAttributes
include ActiveModel::AttributeMethods

def attributes
{ :begin => 'value of begin', :end => 'value of end' }
{ begin: 'value of begin', end: 'value of end' }
end

private
Expand Down
6 changes: 3 additions & 3 deletions activemodel/test/cases/callbacks_test.rb
Expand Up @@ -15,9 +15,9 @@ class ModelCallbacks
extend ActiveModel::Callbacks

define_model_callbacks :create
define_model_callbacks :initialize, :only => :after
define_model_callbacks :multiple, :only => [:before, :around]
define_model_callbacks :empty, :only => []
define_model_callbacks :initialize, only: :after
define_model_callbacks :multiple, only: [:before, :around]
define_model_callbacks :empty, only: []

before_create :before_create
around_create CallbackValidator.new
Expand Down
4 changes: 2 additions & 2 deletions activemodel/test/cases/conversion_test.rb
Expand Up @@ -13,15 +13,15 @@ class ConversionTest < ActiveModel::TestCase
end

test "to_key default implementation returns the id in an array for persisted records" do
assert_equal [1], Contact.new(:id => 1).to_key
assert_equal [1], Contact.new(id: 1).to_key
end

test "to_param default implementation returns nil for new records" do
assert_nil Contact.new.to_param
end

test "to_param default implementation returns a string of ids for persisted records" do
assert_equal "1", Contact.new(:id => 1).to_param
assert_equal "1", Contact.new(id: 1).to_param
end

test "to_partial_path default implementation returns a string giving a relative path" do
Expand Down
8 changes: 4 additions & 4 deletions activemodel/test/cases/errors_test.rb
Expand Up @@ -271,8 +271,8 @@ def test_has_key?

test "add_on_empty generates message with custom default message" do
person = Person.new
person.errors.expects(:generate_message).with(:name, :empty, {:message => 'custom'})
person.errors.add_on_empty :name, :message => 'custom'
person.errors.expects(:generate_message).with(:name, :empty, { message: 'custom' })
person.errors.add_on_empty :name, message: 'custom'
end

test "add_on_empty generates message with empty string value" do
Expand All @@ -297,7 +297,7 @@ def test_has_key?

test "add_on_blank generates message with custom default message" do
person = Person.new
person.errors.expects(:generate_message).with(:name, :blank, {:message => 'custom'})
person.errors.add_on_blank :name, :message => 'custom'
person.errors.expects(:generate_message).with(:name, :blank, { message: 'custom' })
person.errors.add_on_blank :name, message: 'custom'
end
end
4 changes: 2 additions & 2 deletions activemodel/test/cases/model_test.rb
Expand Up @@ -13,7 +13,7 @@ def setup
end

def test_initialize_with_params
object = BasicModel.new(:attr => "value")
object = BasicModel.new(attr: "value")
assert_equal object.attr, "value"
end

Expand All @@ -26,7 +26,7 @@ def test_initialize_with_nil_or_empty_hash_params_does_not_explode
end

def test_persisted_is_always_false
object = BasicModel.new(:attr => "value")
object = BasicModel.new(attr: "value")
assert object.persisted? == false
end
end

0 comments on commit eebb9dd

Please sign in to comment.