From 6fbbdbac5f6747dddb7fa833d7a1cb5ce7a0b3b0 Mon Sep 17 00:00:00 2001 From: Alvaro Bautista Date: Wed, 4 Aug 2010 21:05:29 +0200 Subject: [PATCH] before_save callback fails using update_attribute --- Gemfile.lock | 73 ++++++++++++++++++++++++++++ app/models/lazy.rb | 12 +++++ db/migrate/20100804185632_create_lazies.rb | 14 +++++ db/schema.rb | 22 ++++++++ test/fixtures/lazies.yml | 9 ++++ test/unit/lazy_test.rb | 39 +++++++++++++++ 6 files changed, 169 insertions(+), 0 deletions(-) create mode 100644 Gemfile.lock create mode 100644 app/models/lazy.rb create mode 100644 db/migrate/20100804185632_create_lazies.rb create mode 100644 db/schema.rb create mode 100644 test/fixtures/lazies.yml create mode 100644 test/unit/lazy_test.rb diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..c0e7e37 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,73 @@ +GEM + remote: http://rubygems.org/ + specs: + abstract (1.0.0) + actionmailer (3.0.0.rc) + actionpack (= 3.0.0.rc) + mail (~> 2.2.5) + actionpack (3.0.0.rc) + activemodel (= 3.0.0.rc) + activesupport (= 3.0.0.rc) + builder (~> 2.1.2) + erubis (~> 2.6.6) + i18n (~> 0.4.1) + rack (~> 1.2.1) + rack-mount (~> 0.6.9) + rack-test (~> 0.5.4) + tzinfo (~> 0.3.22) + activemodel (3.0.0.rc) + activesupport (= 3.0.0.rc) + builder (~> 2.1.2) + i18n (~> 0.4.1) + activerecord (3.0.0.rc) + activemodel (= 3.0.0.rc) + activesupport (= 3.0.0.rc) + arel (~> 0.4.0) + tzinfo (~> 0.3.22) + activeresource (3.0.0.rc) + activemodel (= 3.0.0.rc) + activesupport (= 3.0.0.rc) + activesupport (3.0.0.rc) + arel (0.4.0) + activesupport (>= 3.0.0.beta) + builder (2.1.2) + erubis (2.6.6) + abstract (>= 1.0.0) + i18n (0.4.1) + mail (2.2.5) + activesupport (>= 2.3.6) + mime-types + treetop (>= 1.4.5) + mime-types (1.16) + polyglot (0.3.1) + rack (1.2.1) + rack-mount (0.6.9) + rack (>= 1.0.0) + rack-test (0.5.4) + rack (>= 1.0) + rails (3.0.0.rc) + actionmailer (= 3.0.0.rc) + actionpack (= 3.0.0.rc) + activerecord (= 3.0.0.rc) + activeresource (= 3.0.0.rc) + activesupport (= 3.0.0.rc) + bundler (>= 1.0.0.rc.1) + railties (= 3.0.0.rc) + railties (3.0.0.rc) + actionpack (= 3.0.0.rc) + activesupport (= 3.0.0.rc) + rake (>= 0.8.3) + thor (~> 0.14.0) + rake (0.8.7) + sqlite3-ruby (1.3.1) + thor (0.14.0) + treetop (1.4.8) + polyglot (>= 0.3.1) + tzinfo (0.3.22) + +PLATFORMS + ruby + +DEPENDENCIES + rails (= 3.0.0.rc) + sqlite3-ruby diff --git a/app/models/lazy.rb b/app/models/lazy.rb new file mode 100644 index 0000000..cf322dc --- /dev/null +++ b/app/models/lazy.rb @@ -0,0 +1,12 @@ +class Lazy < ActiveRecord::Base + + before_save :upcase_data + + private + + def upcase_data + self.band.upcase! + self.title.upcase! + end + +end diff --git a/db/migrate/20100804185632_create_lazies.rb b/db/migrate/20100804185632_create_lazies.rb new file mode 100644 index 0000000..513b0b0 --- /dev/null +++ b/db/migrate/20100804185632_create_lazies.rb @@ -0,0 +1,14 @@ +class CreateLazies < ActiveRecord::Migration + def self.up + create_table :lazies do |t| + t.string :band + t.string :title + + t.timestamps + end + end + + def self.down + drop_table :lazies + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..882b755 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,22 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended to check this file into your version control system. + +ActiveRecord::Schema.define(:version => 20100804185632) do + + create_table "lazies", :force => true do |t| + t.string "band" + t.string "title" + t.datetime "created_at" + t.datetime "updated_at" + end + +end diff --git a/test/fixtures/lazies.yml b/test/fixtures/lazies.yml new file mode 100644 index 0000000..a22045b --- /dev/null +++ b/test/fixtures/lazies.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +one: + band: MyString + title: MyString + +two: + band: MyString + title: MyString diff --git a/test/unit/lazy_test.rb b/test/unit/lazy_test.rb new file mode 100644 index 0000000..992bcdf --- /dev/null +++ b/test/unit/lazy_test.rb @@ -0,0 +1,39 @@ +require 'test_helper' + +class LazyTest < ActiveSupport::TestCase + + test "before_save with create" do + lazy = Lazy.create(:band => "Lovin' Spoonful", :title => "Do you believe in magic") + assert_equal "LOVIN' SPOONFUL", lazy.band + assert_equal "DO YOU BELIEVE IN MAGIC", lazy.title + end + + test "before_save with save" do + lazy = Lazy.create(:band => "Lovin' Spoonful", :title => "Do you believe in magic") + + lazy.band = "The Lovin' Spoonful" + lazy.title = "Daydream" + lazy.save + + assert_equal "THE LOVIN' SPOONFUL", lazy.band + assert_equal "DAYDREAM", lazy.title + end + + test "before_save with update_attributes" do + lazy = Lazy.create(:band => "Lovin' Spoonful", :title => "Do you believe in magic") + + lazy.update_attributes(:band => "The Lovin' Spoonful", :title => "Daydream") + assert_equal "THE LOVIN' SPOONFUL", lazy.band + assert_equal "DAYDREAM", lazy.title + end + + test "before_save with update_attribute" do + lazy = Lazy.create(:band => "Lovin' Spoonful", :title => "Do you believe in magic") + + lazy.update_attribute(:band, "The Lovin' Spoonful") # FAILS + lazy.update_attribute(:title, "Daydream") + assert_equal "THE LOVIN' SPOONFUL", lazy.band + assert_equal "DAYDREAM", lazy.title + end + +end -- 1.6.4.2