Adds suppport for ActiveModel::Dirty and ::AttributeMethods

* ActiveModel::Dirty
** Basic support for dirty tracking
** It does not bubble up any changes to casted models currently

* ActiveModel::AttributeMethods
** Attributes are now read and written through ActiveModel
** This also allows you to add your own attribute methods with
   prefix suffix and affix names. For more information check out
   ActiveModel::AttributeMethods::ClassMethods
This commit is contained in:
Will Leinweber 2010-09-16 17:24:43 -05:00
parent 5c21de8586
commit d333133319
12 changed files with 333 additions and 124 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@ pkg
Gemfile*
.rvmrc
.bundle
couchdb.std*

View file

@ -1,6 +1,8 @@
== Next Version
* Major enhancements
* Dirty Tracking via ActiveModel
* ActiveModel Attribute Methods support
* Minor enhancements
* Fixing find("") issue (thanks epochwolf)

View file

@ -1 +1 @@
require File.join(File.dirname(__FILE__),'lib', 'couchrest', 'extended_document')
require File.join(File.dirname(__FILE__),'lib', 'couchrest', 'model')

View file

@ -1,13 +1,62 @@
module CouchRest
module Model
ReadOnlyPropertyError = Class.new(StandardError)
# Attributes Suffixes provide methods from ActiveModel
# to hook into. See methods such as #attribute= and
# #attribute? for their implementation
AttributeMethodSuffixes = ['', '=', '?']
module Attributes
extend ActiveSupport::Concern
included do
include ActiveModel::AttributeMethods
attribute_method_suffix *AttributeMethodSuffixes
end
module ClassMethods
def attributes
properties.map {|prop| prop.name}
end
end
def initialize(*args)
self.class.attribute_method_suffix *AttributeMethodSuffixes
super
end
def attributes
self.class.attributes
end
## Reads the attribute value.
# Assuming you have a property :title this would be called
# by `model_instance.title`
def attribute(name)
read_attribute(name)
end
## Sets the attribute value.
# Assuming you have a property :title this would be called
# by `model_instance.title = 'hello'`
def attribute=(name, value)
raise ReadOnlyPropertyError, 'read only property' if find_property!(name).read_only
write_attribute(name, value)
end
## Tests for both presence and truthiness of the attribute.
# Assuming you have a property :title # this would be called
# by `model_instance.title?`
def attribute?(name)
value = read_attribute(name)
!(value.nil? || value == false)
end
## Support for handling attributes
#
# This would be better in the properties file, but due to scoping issues
# this is not yet possible.
#
def prepare_all_attributes(doc = {}, options = {})
apply_all_property_defaults
if options[:directly_set_attributes]
@ -29,11 +78,26 @@ module CouchRest
end
alias :attributes= :update_attributes_without_saving
def read_attribute(property)
prop = find_property!(property)
self[prop.to_s]
end
def write_attribute(property, value)
prop = find_property!(property)
self[prop.to_s] = prop.cast(self, value)
end
private
def read_only_attributes
properties.select { |prop| prop.read_only }.map { |prop| prop.name }
end
def directly_set_attributes(hash)
r_o_a = read_only_attributes
hash.each do |attribute_name, attribute_value|
next if r_o_a.include? attribute_name
if self.respond_to?("#{attribute_name}=")
self.send("#{attribute_name}=", hash.delete(attribute_name))
end
@ -41,9 +105,10 @@ module CouchRest
end
def directly_set_read_only_attributes(hash)
property_list = self.properties.map{|p| p.name}
r_o_a = read_only_attributes
property_list = attributes
hash.each do |attribute_name, attribute_value|
next if self.respond_to?("#{attribute_name}=")
next unless r_o_a.include? attribute_name
if property_list.include?(attribute_name)
write_attribute(attribute_name, hash.delete(attribute_name))
end
@ -56,12 +121,11 @@ module CouchRest
end
def check_properties_exist(attrs)
property_list = self.properties.map{|p| p.name}
property_list = attributes
attrs.each do |attribute_name, attribute_value|
raise NoMethodError, "Property #{attribute_name} not created" unless respond_to?("#{attribute_name}=") or property_list.include?(attribute_name)
end
end
end
end
end

View file

@ -16,6 +16,7 @@ module CouchRest
include CouchRest::Model::Attributes
include CouchRest::Model::Associations
include CouchRest::Model::Validations
include CouchRest::Model::Dirty
def self.subclasses
@subclasses ||= []

View file

@ -0,0 +1,44 @@
# encoding: utf-8
require 'active_model/dirty'
module CouchRest #:nodoc:
module Model #:nodoc:
# Dirty Tracking support via ActiveModel
# mixin methods include:
# #changed?, #changed, #changes, #previous_changes
# #<attribute>_changed?, #<attribute>_change,
# #reset_<attribute>!, #<attribute>_will_change!,
# and #<attribute>_was
#
# Please see the specs or the documentation of
# ActiveModel::Dirty for more information
module Dirty
extend ActiveSupport::Concern
included do
include ActiveModel::Dirty
after_save :clear_changed_attributes
end
def initialize(*args)
super
@changed_attributes.clear if @changed_attributes
end
def write_attribute(name, value)
meth = :"#{name}_will_change!"
__send__ meth if respond_to? meth
super
end
private
def clear_changed_attributes
@previously_changed = changes
@changed_attributes.clear
true
end
end
end
end

View file

@ -1,4 +1,5 @@
# encoding: utf-8
require 'set'
module CouchRest
module Model
module Properties
@ -22,16 +23,6 @@ module CouchRest
self.class.properties
end
def read_attribute(property)
prop = find_property!(property)
self[prop.to_s]
end
def write_attribute(property, value)
prop = find_property!(property)
self[prop.to_s] = prop.cast(self, value)
end
def apply_all_property_defaults
return if self.respond_to?(:new?) && (new? == false)
# TODO: cache the default object
@ -94,8 +85,7 @@ module CouchRest
type = [type] # inject as an array
end
property = Property.new(name, type, options)
create_property_getter(property)
create_property_setter(property) unless property.read_only == true
create_property_alias(property) if property.alias
if property.type_class.respond_to?(:validates_casted_model)
validates_casted_model property.name
end
@ -103,49 +93,15 @@ module CouchRest
property
end
# defines the getter for the property (and optional aliases)
def create_property_getter(property)
# meth = property.name
def create_property_alias(property)
class_eval <<-EOS, __FILE__, __LINE__ + 1
def #{property.name}
read_attribute('#{property.name}')
def #{property.alias.to_s}
#{property.name}
end
EOS
if ['boolean', TrueClass.to_s.downcase].include?(property.type.to_s.downcase)
class_eval <<-EOS, __FILE__, __LINE__
def #{property.name}?
value = read_attribute('#{property.name}')
!(value.nil? || value == false)
end
EOS
end
if property.alias
class_eval <<-EOS, __FILE__, __LINE__ + 1
alias #{property.alias.to_sym} #{property.name.to_sym}
EOS
end
end
# defines the setter for the property (and optional aliases)
def create_property_setter(property)
property_name = property.name
class_eval <<-EOS
def #{property_name}=(value)
write_attribute('#{property_name}', value)
end
EOS
if property.alias
class_eval <<-EOS
alias #{property.alias.to_sym}= #{property_name.to_sym}=
EOS
end
end
end # module ClassMethods
end
end
end

View file

@ -48,6 +48,7 @@ require "couchrest/model/collection"
require "couchrest/model/attribute_protection"
require "couchrest/model/attributes"
require "couchrest/model/associations"
require "couchrest/model/dirty"
# Monkey patches applied to couchrest
require "couchrest/model/support/couchrest"

View file

@ -0,0 +1,126 @@
require File.expand_path("../../spec_helper", __FILE__)
class DirtyModel < CouchRest::Model::Base
use_database TEST_SERVER.default_database
property :name
property :color
validates_presence_of :name
end
describe 'Dirty Tracking', '#changed?' do
before(:each) do
@dm = DirtyModel.new
@dm.name = 'will'
end
it 'brand new models should not be changed by default' do
DirtyModel.new.should_not be_changed
end
it 'save should reset changed?' do
@dm.should be_changed
@dm.save
@dm.should_not be_changed
end
it 'save! should reset changed?' do
@dm.should be_changed
@dm.save!
@dm.should_not be_changed
end
it 'a failed save should preserve changed?' do
@dm.name = ''
@dm.should be_changed
@dm.save.should be_false
@dm.should be_changed
end
it 'should be true if there have been changes' do
@dm.name = 'not will'
@dm.should be_changed
end
end
describe 'Dirty Tracking', '#changed' do
it 'should be an array of the changed attributes' do
dm = DirtyModel.new
dm.changed.should == []
dm.name = 'will'
dm.changed.should == ['name']
dm.color = 'red'
dm.changed.should =~ ['name', 'color']
end
end
describe 'Dirty Tracking', '#changes' do
it 'should be a Map of changed attrs => [original value, new value]' do
dm = DirtyModel.new(:name => 'will', :color => 'red')
dm.save!
dm.should_not be_changed
dm.name = 'william'
dm.color = 'blue'
dm.changes.should == { 'name' => ['will', 'william'], 'color' => ['red', 'blue'] }
end
end
describe 'Dirty Tracking', '#previous_changes' do
it 'should store the previous changes after a save' do
dm = DirtyModel.new(:name => 'will', :color => 'red')
dm.save!
dm.should_not be_changed
dm.name = 'william'
dm.save!
dm.previous_changes.should == { 'name' => ['will', 'william'] }
end
end
describe 'Dirty Tracking', 'attribute methods' do
before(:each) do
@dm = DirtyModel.new(:name => 'will', :color => 'red')
@dm.save!
end
describe '#<attr>_changed?' do
it 'it should know if a specific property was changed' do
@dm.name = 'william'
@dm.should be_name_changed
@dm.should_not be_color_changed
end
end
describe 'Dirty Tracking', '#<attr>_change' do
it 'should be an array of [original value, current value]' do
@dm.name = 'william'
@dm.name_change.should == ['will', 'william']
end
end
describe 'Dirty Tracking', '#<attr>_was' do
it 'should return what the attribute was' do
@dm.name = 'william'
@dm.name_was.should == 'will'
end
end
describe 'Dirty Tracking', '#reset_<attr>!' do
it 'should reset the attribute to what it was' do
@dm.name = 'william'
@dm.reset_name!
@dm.name.should == 'will'
end
end
describe 'Dirty Tracking', '#<attr>_will_change!' do
it 'should manually mark the attribute as changed' do
@dm.should_not be_name_changed
@dm.name_will_change!
@dm.should be_name_changed
end
end
end

View file

@ -9,6 +9,20 @@ require File.join(FIXTURE_PATH, 'more', 'event')
require File.join(FIXTURE_PATH, 'more', 'user')
require File.join(FIXTURE_PATH, 'more', 'course')
describe 'Attributes' do
class AttrDoc < CouchRest::Model::Base
property :one
property :two
end
it '.attributes should have an array of attribute names' do
AttrDoc.attributes.should =~ ['two', 'one']
end
it '#attributes should have an array of attribute names' do
AttrDoc.new.attributes.should =~ ['two', 'one']
end
end
describe "Model properties" do