Kouhei Sutou
null+****@clear*****
Thu Dec 22 17:14:18 JST 2016
Kouhei Sutou 2016-12-22 17:14:18 +0900 (Thu, 22 Dec 2016) New Revision: d1a168ad01289ca85a8230d8bed00709766aa72e https://github.com/ranguba/groonga-client-model/commit/d1a168ad01289ca85a8230d8bed00709766aa72e Message: Pass validation info to sub records Modified files: lib/groonga_client_model/record.rb test/unit/test_record.rb Modified: lib/groonga_client_model/record.rb (+21 -12) =================================================================== --- lib/groonga_client_model/record.rb 2016-12-22 16:57:34 +0900 (b66f743) +++ lib/groonga_client_model/record.rb 2016-12-22 17:14:18 +0900 (55c5923) @@ -201,12 +201,12 @@ module GroongaClientModel def save_raw(validate:) if validate if valid? - upsert + upsert(validate: true) else false end else - upsert + upsert(validate: false) end end @@ -227,20 +227,21 @@ module GroongaClientModel freeze end - def upsert + def upsert(validate:) if new_record? run_callbacks(:create) do - upsert_raw + upsert_raw(validate: validate) end else run_callbacks(:update) do - upsert_raw + upsert_raw(validate: validate) end end end - def upsert_raw - upsert_sub_records + def upsert_raw(validate:) + return false unless upsert_sub_records(validate: validate) + Client.open do |client| table = self.class.schema.tables[self.class.table_name] load_value_generator = LoadValueGenerator.new(self) @@ -279,20 +280,28 @@ module GroongaClientModel end end - def upsert_sub_records + def upsert_sub_records(validate:) attributes.each do |name, value| - upsert_sub_record(value) + return false unless upsert_sub_record(value, validate) end + true end - def upsert_sub_record(sub_record) + def upsert_sub_record(sub_record, validate) case sub_record when Record - sub_record.save if sub_record.changed? + if sub_record.changed? + sub_record.save(validate: validate) + else + true + end when Array sub_record.each do |sub_element| - upsert_sub_record(sub_element) + unless upsert_sub_record(sub_element, validate) + return false + end end + true end end Modified: test/unit/test_record.rb (+6 -2) =================================================================== --- test/unit/test_record.rb 2016-12-22 16:57:34 +0900 (be41450) +++ test/unit/test_record.rb 2016-12-22 17:14:18 +0900 (a3d0d76) @@ -72,13 +72,17 @@ class TestRecord < Test::Unit::TestCase test "no key" do record = NoKey.new - record.validate + assert do + record.valid? + end assert_equal({}, record.errors.messages) end test "have key" do record = HaveKey.new - record.validate + assert do + not record.save + end assert_equal({ :_key => [record.errors.generate_message(:_key, :blank)], }, -------------- next part -------------- HTML����������������������������... 下载