This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #1222 dm-rails doesn't play nice with date_select
- Loading branch information
1 parent
d501dd9
commit 0b868c1
Showing
7 changed files
with
214 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
module Rails | ||
module DataMapper | ||
module MultiparameterAttribute | ||
def self.included(model) | ||
model.alias_method_chain :attributes=, :multiparameter | ||
end | ||
|
||
protected | ||
def attributes_with_multiparameter=(values_hash) | ||
attribs = values_hash.dup | ||
multi_parameter_attributes = [] | ||
attribs.each do |k, v| | ||
if k.to_s.include?("(") | ||
multi_parameter_attributes << [ k, v ] | ||
attribs.delete(k) | ||
# else | ||
# respond_to?(:"#{k}=") ? send(:"#{k}=", v) : raise(ArgumentError, "unknown property: #{k}") | ||
end | ||
end | ||
|
||
attribs = attribs.merge(assign_multiparameter_attributes(multi_parameter_attributes)) | ||
|
||
self.attributes_without_multiparameter = attribs | ||
end | ||
|
||
def assign_multiparameter_attributes(pairs) | ||
execute_callstack_for_multiparameter_attributes( | ||
extract_callstack_for_multiparameter_attributes(pairs) | ||
) | ||
end | ||
|
||
def instantiate_time_object(name, values) | ||
if properties[name].name.demodulize =~ /utc/i | ||
Time.zone.local(*values) | ||
else | ||
Time.time_with_datetime_fallback(Rails.configuration.time_zone, *values) | ||
end | ||
end | ||
|
||
def execute_callstack_for_multiparameter_attributes(callstack) | ||
errors = [] | ||
attribs = {} | ||
callstack.each do |name, values| | ||
klass = properties[name].primitive | ||
if values.empty? | ||
attribs[name] = nil | ||
else | ||
begin | ||
value = if Time == klass || DateTime == klass | ||
instantiate_time_object(name, values) | ||
elsif Date == klass | ||
begin | ||
Date.new(*values) | ||
rescue ArgumentError => ex # if Date.new raises an exception on an invalid date | ||
instantiate_time_object(name, values).to_date # we instantiate Time object and convert it back to a date thus using Time's logic in handling invalid dates | ||
end | ||
else | ||
klass.new(*values) | ||
end | ||
|
||
attribs[name] = value | ||
rescue => ex | ||
errors << "error on assignment #{values.inspect} to #{name}" | ||
end | ||
end | ||
end | ||
unless errors.empty? | ||
raise(ArgumentError, "#{errors.size} error(s) on assignment of multiparameter attributes") | ||
end | ||
attribs | ||
end | ||
|
||
def extract_callstack_for_multiparameter_attributes(pairs) | ||
attributes = { } | ||
|
||
for pair in pairs | ||
multiparameter_name, value = pair | ||
attribute_name = multiparameter_name.split("(").first | ||
attributes[attribute_name] = [] unless attributes.include?(attribute_name) | ||
|
||
unless value.empty? | ||
attributes[attribute_name] << | ||
[ find_parameter_position(multiparameter_name), type_cast_attribute_value(multiparameter_name, value) ] | ||
end | ||
end | ||
|
||
attributes.each { |name, values| attributes[name] = values.sort_by{ |v| v.first }.collect { |v| v.last } } | ||
end | ||
|
||
|
||
def type_cast_attribute_value(multiparameter_name, value) | ||
multiparameter_name =~ /\([0-9]*([a-z])\)/ ? value.send("to_" + $1) : value | ||
end | ||
|
||
def find_parameter_position(multiparameter_name) | ||
multiparameter_name.scan(/\(([0-9]*).*\)/).first.first | ||
end | ||
|
||
end # Model | ||
end | ||
|
||
end | ||
|
||
DataMapper::Model.append_inclusions(Rails::DataMapper::MultiparameterAttribute) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--format progress | ||
--color | ||
--backtrace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
$:.unshift File.expand_path File.dirname(__FILE__) + '../lib' | ||
require "dm-migrations" | ||
require 'dm-core/spec/setup' | ||
require 'dm-core/spec/lib/adapter_helpers' | ||
require 'dm-core/spec/lib/spec_helper' | ||
require 'dm-core/spec/lib/pending_helpers' | ||
|
||
Spec::Runner.configure do |config| | ||
|
||
config.extend(DataMapper::Spec::Adapters::Helpers) | ||
config.include(DataMapper::Spec::PendingHelpers) | ||
|
||
config.after :all do | ||
DataMapper::Spec.cleanup_models | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'spec_helper' | ||
require "dm-rails/multiparameter_attribute_support" | ||
|
||
describe "Multiparameter attributes (ie dates) in rails forms" do | ||
|
||
supported_by :all do | ||
before(:all) do | ||
class MuliparameterRecource | ||
include DataMapper::Resource | ||
|
||
property :id, Serial | ||
property :name, String | ||
property :birthday, Date | ||
end | ||
MuliparameterRecource.auto_migrate! | ||
end | ||
|
||
it "should not blow up" do | ||
lambda { create_mpr }.should_not raise_error | ||
end | ||
|
||
it "should assign multiparameter property correctly" do | ||
mpr = create_mpr | ||
mpr.birthday.should == Date.new(1989,3,26) | ||
end | ||
|
||
it "should still assign the regular values" do | ||
mpr = create_mpr | ||
mpr.name.should == "joe" | ||
end | ||
|
||
it "should not change the attributes hash" do | ||
mpr = create_mpr | ||
mpr.attributes.should == {:birthday => Date.new(1989,3,26), :name => "joe", :id => mpr.id} | ||
end | ||
|
||
it "should not change non-multiparameter attributes" do | ||
mpr = MuliparameterRecource.new | ||
attributes = { "birthday" => Date.new(1989,3,26), "name" => "joe" } | ||
mpr.should_receive(:attributes_without_multiparameter=).with(attributes.dup) # dup to make sure original hash isn't modified | ||
mpr.send(:attributes_with_multiparameter=, multiparameter_hash).should == attributes | ||
end | ||
end | ||
|
||
def multiparameter_hash | ||
{ | ||
"birthday(2i)"=>"3", | ||
"birthday(3i)"=>"26", | ||
"birthday(1i)"=>"1989", | ||
"name" => "joe" | ||
} | ||
end | ||
|
||
def create_mpr | ||
MuliparameterRecource.create(multiparameter_hash) | ||
end | ||
|
||
end | ||
|