lib/facemock/database/table.rb in facemock-0.0.9 vs lib/facemock/database/table.rb in facemock-0.0.10
- old
+ new
@@ -212,11 +212,10 @@
def self.define_find_method(method_name, column_name)
case method_name
when /^find_by_(.+)/ then define_find_by_column(column_name)
when /^find_all_by_(.+)/ then define_find_all_by_column(column_name)
- else false
end
end
def self.define_find_by_column(column_name)
self.class_eval <<-EOF
@@ -277,18 +276,16 @@
# DatabaseへのINSERTが成功してからインスタンスのフィールド値を更新する
def insert!(options={})
opts = Hashie::Mash.new(options)
instance = self.class.new
column_names.each do |column_name|
- if column_name != :created_at
- if self.class.column_notnull(column_name) && column_is_empty?(column_name)
- raise Facemock::Errors::ColumnTypeNotNull, "#{column_name} is null"
- end
- instance.send(column_name.to_s + "=", self.send(column_name))
- if opts.send(column_name)
- instance.send(column_name.to_s + "=", opts.send(column_name))
- end
+ next if column_name == :created_at
+ notnull_check(column_name)
+ instance.send(column_name.to_s + "=", self.send(column_name))
+
+ if opts.send(column_name)
+ instance.send(column_name.to_s + "=", opts.send(column_name))
end
end
target_column_names = if instance.id
column_names
@@ -348,9 +345,15 @@
end
def self.column_notnull(column_name)
return nil unless column_names.include?(column_name.to_s.to_sym)
table_info.send(column_name).notnull
+ end
+
+ def notnull_check(column_name)
+ if self.class.column_notnull(column_name) && column_is_empty?(column_name)
+ raise Facemock::Errors::ColumnTypeNotNull, "#{column_name} is null"
+ end
end
end
end
end