Skip to content
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ See the [update history](https://github.com/couchrest/couchrest_model/blob/maste

### Upgrading from an earlier version?

*Pre 2.2:* As of August 2016, dirty tracking has been radically re-factored away from ActiveModel::Dirty, which only has support for basic attributes, into a solution that uses [Hashdiff](https://github.com/liufengyun/hashdiff), more details available in the [pull request](https://github.com/couchrest/couchrest_model/pull/211). The result is that some of ActiveModel's Dirty methods are no longer available, these are: `changes_applied`, `restore_attributes`, `previous_changes`, and `changed_attributes`.

*Pre 2.0:* As of June 2012, couchrest model no longer supports the `view_by` and `view` calls from the model. Views are no only accessed via a design document. If you have older code and wish to upgrade, please ensure you move to the new syntax for using views.

*Pre 1.1:* As of April 2011 and the release of version 1.1.0, the default model type key is 'type' instead of 'couchrest-type'. Simply updating your project will not work unless you migrate your data or set the configuration option in your initializers:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.0.rc1
2.2.0.beta
1 change: 1 addition & 0 deletions couchrest_model.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Gem::Specification.new do |s|
s.add_dependency("couchrest", "2.0.0")
s.add_dependency("activemodel", "~> 4.0")
s.add_dependency("tzinfo", ">= 0.3.22")
s.add_dependency("hashdiff", "~> 0.3")
s.add_development_dependency("rspec", "~> 2.14.1")
s.add_development_dependency("rack-test", ">= 0.5.7")
s.add_development_dependency("rake", ">= 0.8.0")
Expand Down
6 changes: 5 additions & 1 deletion history.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# CouchRest Model Change History

## 2.1.0 - 2016-07-06
## 2.2.0.beta1 - pending

* Radical re-factor of dirty tracking using Hashdiff gem, providing reliable change detection with nested data. [PR](https://github.com/couchrest/couchrest_model/pull/211) (@samlown)

## 2.1.0.rc1 - 2016-07-06

* Adding "persistent" connection configuration property (@samlown)
* Releasing 2.1.0 off of CouchRest 2.0.0.
Expand Down
11 changes: 3 additions & 8 deletions lib/couchrest/model/associations.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
module CouchRest
module Model
# Basic support for relationships between CouchRest::Model::Base
module Associations

# Basic support for relationships between CouchRest::Model::Base

def self.included(base)
base.extend(ClassMethods)
end
extend ActiveSupport::Concern

module ClassMethods

Expand Down Expand Up @@ -240,8 +236,7 @@ def check_obj(obj)

# Override CastedArray instantiation_and_cast method for a simpler
# version that will not try to cast the model.
def instantiate_and_cast(obj, change = true)
couchrest_parent_will_change! if change && use_dirty?
def instantiate_and_cast(obj)
obj.casted_by = casted_by if obj.respond_to?(:casted_by)
obj.casted_by_property = casted_by_property if obj.respond_to?(:casted_by_property)
obj
Expand Down
22 changes: 15 additions & 7 deletions lib/couchrest/model/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,34 @@ def self.inherited(subklass)
#
# Options supported:
#
# * :directly_set_attributes, true when data comes directly from database
# * :write_all_attributes, true when data comes directly from database so we can set protected and read-only attributes.
# * :database, provide an alternative database
#
# If a block is provided the new model will be passed into the
# block so that it can be populated.
def initialize(attributes = {}, options = {})
def initialize(attributes = {}, options = {}, &block)
super()
prepare_all_attributes(attributes, options)
# set the instance's database, if provided

# Always force the type of model
self[self.model_type_key] = self.class.model_type_value

# Some instances may require a different database
self.database = options[:database] unless options[:database].nil?
unless self['_id'] && self['_rev']
self[self.model_type_key] = self.class.model_type_value
end

# Deal with the attributes
write_attributes_for_initialization(attributes, options)

yield self if block_given?

after_initialize if respond_to?(:after_initialize)
run_callbacks(:initialize) { self }
end

def self.build(attrs = {}, options = {}, &block)


end

alias :new_record? :new?
alias :new_document? :new?

Expand Down
51 changes: 20 additions & 31 deletions lib/couchrest/model/casted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

module CouchRest::Model
class CastedArray < Array
include CouchRest::Model::Configuration
include CouchRest::Model::CastedBy
include CouchRest::Model::Dirty
attr_accessor :casted_by_property
Expand All @@ -30,42 +31,15 @@ def unshift(obj)
end

def []= index, obj
value = instantiate_and_cast(obj, false)
couchrest_parent_will_change! if use_dirty? && value != self[index]
value = instantiate_and_cast(obj)
super(index, value)
end

def insert index, *args
values = *args.map{|obj| instantiate_and_cast(obj, false)}
couchrest_parent_will_change! if use_dirty?
values = *args.map{|obj| instantiate_and_cast(obj)}
super(index, *values)
end

def pop
couchrest_parent_will_change! if use_dirty? && self.length > 0
super
end

def shift
couchrest_parent_will_change! if use_dirty? && self.length > 0
super
end

def clear
couchrest_parent_will_change! if use_dirty? && self.length > 0
super
end

def delete(obj)
couchrest_parent_will_change! if use_dirty? && self.length > 0
super(obj)
end

def delete_at(index)
couchrest_parent_will_change! if use_dirty? && self.length > 0
super(index)
end

def build(*args)
obj = casted_by_property.build(*args)
self.push(obj)
Expand All @@ -76,11 +50,26 @@ def as_couch_json
map{ |v| (v.respond_to?(:as_couch_json) ? v.as_couch_json : v)}
end

# Overwrite the standard dirty tracking clearing.
# We don't have any properties, but we do need to check
# entries in our array.
def clear_changes_information
if use_dirty?
each do |val|
if val.respond_to?(:clear_changes_information)
val.clear_changes_information
end
end
@original_change_data = current_change_data
else
@original_change_data = nil
end
end

protected

def instantiate_and_cast(obj, change = true)
def instantiate_and_cast(obj)
property = casted_by_property
couchrest_parent_will_change! if change && use_dirty?
if casted_by && property && obj.class != property.type
property.cast_value(casted_by, obj)
else
Expand Down
84 changes: 0 additions & 84 deletions lib/couchrest/model/casted_hash.rb

This file was deleted.

2 changes: 2 additions & 0 deletions lib/couchrest/model/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ module Configuration
add_config :connection
add_config :connection_config_file
add_config :time_fraction_digits
add_config :disable_dirty_tracking

configure do |config|
config.model_type_key = 'type' # was 'couchrest-type'
config.mass_assign_any_attribute = false
config.auto_update_design_doc = true
config.time_fraction_digits = 3
config.disable_dirty_tracking = false

config.environment = :development
config.connection_config_file = File.join(Dir.pwd, 'config', 'couchdb.yml')
Expand Down
Loading