Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wagenet committed May 22, 2009
0 parents commit 837e26c
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 0 deletions.
20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2009 [name of plugin creator]

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15 changes: 15 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
MultiAssignmentSanity
====================

Fixes issues with Multi-parameter Assignment failure


Example
=======

class Person < ActiveRecord::Base
sanitize_multi_assignment_errors :message => "{attr} has error: {error}"
end


Copyright (c) 2009 Peter Wagenet, released under the MIT license
22 changes: 22 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the mass_assignment_sanity plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the mass_assignment_sanity plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'MassAssignmentSanity'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
1 change: 1 addition & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'multi_assignment_sanity'
45 changes: 45 additions & 0 deletions lib/multi_assignment_sanity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module MultiAssignmentSanity

def self.included(base)
base.extend MultiAssignmentSanity::ActsMethods
end

module ActsMethods

def sanitize_multi_assignment_errors(options = {})
unless self.include?(MultiAssignmentSanity::InstanceMethods)
include MultiAssignmentSanity::InstanceMethods

cattr_accessor :multi_assignment_sanity_options
self.multi_assignment_sanity_options = options
self.multi_assignment_sanity_options[:message] ||= "has error: {error}"

validate :multi_parameter_errors
end
end

end

module InstanceMethods

def assign_multiparameter_attributes(pairs)
super
rescue ActiveRecord::MultiparameterAssignmentErrors => e
@multi_parameter_errors = e.errors
end

def multi_parameter_errors
return unless @multi_parameter_errors.is_a?(Array)
@multi_parameter_errors.each do |error|
message = self.class.multi_assignment_sanity_options[:message]
message.gsub!('{attr}', error.attribute.titleize)
message.gsub!('{error}', error.exception.message)
self.errors.add(error.attribute, message)
end
@multi_parameter_errors = []
end

end

end
ActiveRecord::Base.send(:include, MultiAssignmentSanity)
4 changes: 4 additions & 0 deletions tasks/mass_assignment_sanity_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :mass_assignment_sanity do
# # Task goes here
# end
29 changes: 29 additions & 0 deletions test/multi_assignment_sanity_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
$LOAD_PATH << File.dirname(__FILE__) + '/../lib'

require 'rubygems'
require 'test/unit'

# Use an earlier version of Rails where date still fails since this makes our testing easier
gem 'activerecord', '<= 2.0.2'

require 'active_record'
require File.dirname(__FILE__) + '/../init'

class MultiAssignmentModel < ActiveRecord::Base
# So we don't require an actual table
def self.columns
[ActiveRecord::ConnectionAdapters::Column.new('date', nil, 'date')]
end

sanitize_multi_assignment_errors
end

class MultiAssignmentSanityTest < Test::Unit::TestCase

def test_sanitize_multi_assignment_errors
obj = MultiAssignmentModel.new("date(1i)" => "2009", "date(2i)" => "6", "date(3i)" => "31")
assert !obj.valid?
assert_equal "has error: invalid date", obj.errors.on(:date)
end

end

0 comments on commit 837e26c

Please sign in to comment.