Skip to content
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

raise RuntimeError for not allowed model properties #199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/couchrest/model/properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def directly_set_attributes(hash, mass_assign = false)
multi_parameter_attributes << [ key, value ]
false
elsif self.respond_to?("#{key}=")
self.send("#{key}=", value)
self.send("#{key}=", value)
elsif mass_assign || mass_assign_any_attribute
couchrest_attribute_will_change!(key) if use_dirty? && self[key] != value
self[key] = value
Expand Down Expand Up @@ -142,7 +142,7 @@ def assign_multiparameter_attributes(pairs, hash)
def execute_callstack_for_multiparameter_attributes(callstack, hash)
callstack.each do |name, values_with_empty_parameters|
if self.respond_to?("#{name}=")
casted_attrib = send("#{name}=", values_with_empty_parameters)
casted_attrib = send("#{name}=", values_with_empty_parameters)
unless casted_attrib.is_a?(Hash)
hash.reject { |key, value| key.include?(name.to_s)}
end
Expand Down Expand Up @@ -171,6 +171,7 @@ def find_parameter_name(multiparameter_name)
module ClassMethods

def property(name, *options, &block)
raise "Invalid property definition, '#{name}' already used internally by CouchRest Model" if name.to_s == 'properties'
raise "Invalid property definition, '#{name}' already used for CouchRest Model type field" if name.to_s == model_type_key.to_s && CouchRest::Model::Base >= self
opts = { }
type = options.shift
Expand All @@ -188,7 +189,7 @@ def property(name, *options, &block)

# Automatically set <tt>updated_at</tt> and <tt>created_at</tt> fields
# on the document whenever saving occurs.
#
#
# These properties are casted as Time objects, so they should always
# be set to UTC.
def timestamps!
Expand Down Expand Up @@ -255,4 +256,3 @@ def create_property_setter(property)
end
end
end

39 changes: 26 additions & 13 deletions spec/unit/properties_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,39 @@

describe CouchRest::Model::Properties do

before(:each) do
@obj = WithDefaultValues.new
describe '.property' do
before do
stub_const 'Invalid', Class.new(CouchRest::Model::Base)
end

it "raises an exception when the model has a property named 'properties'" do
expect do
Invalid.class_eval{ property :properties }
end.to raise_error(RuntimeError, /'properties' already used internally by CouchRest Model/)
end

it "raises an exception when the model has a property named 'type'" do
expect do
Invalid.class_eval{ property :type }
end.to raise_error(RuntimeError, /type' already used for CouchRest Model type field/)
end
end

describe "multipart attributes" do
context "with valid params" do
context "with valid params" do
it "should parse a legal date" do
valid_date_params = { "exec_date(1i)"=>"2011",
"exec_date(2i)"=>"10",
valid_date_params = { "exec_date(1i)"=>"2011",
"exec_date(2i)"=>"10",
"exec_date(3i)"=>"18"}
@obj = WithDateAndTime.new valid_date_params
@obj.exec_date.should_not be_nil
@obj.exec_date.should be_kind_of(Date)
@obj.exec_date.should == Date.new(2011, 10 ,18)
end

it "should parse a legal time" do
valid_time_params = { "exec_time(1i)"=>"2011",
"exec_time(2i)"=>"10",
valid_time_params = { "exec_time(1i)"=>"2011",
"exec_time(2i)"=>"10",
"exec_time(3i)"=>"18",
"exec_time(4i)"=>"15",
"exec_time(5i)"=>"15",
Expand All @@ -32,11 +46,11 @@
@obj.exec_time.should == Time.utc(2011, 10 ,18, 15, 15, 15)
end
end

context "with invalid params" do
before(:each) do
@invalid_date_params = { "exec_date(1i)"=>"2011",
"exec_date(2i)"=>"foo",
@invalid_date_params = { "exec_date(1i)"=>"2011",
"exec_date(2i)"=>"foo",
"exec_date(3i)"=>"18"}
end
it "should still create a model if there are invalid attributes" do
Expand Down Expand Up @@ -74,4 +88,3 @@
end

end