Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model type key modification #163

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions lib/couchrest/model/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def self.inherited(subklass)
subclasses << subklass
end

def self.model_type_string
to_s.send(model_type_modification)
end

# Instantiate a new CouchRest::Model::Base by preparing all properties
# using the provided document hash.
#
Expand All @@ -54,7 +58,7 @@ def initialize(attributes = {}, options = {})
# set the instance's database, if provided
self.database = options[:database] unless options[:database].nil?
unless self['_id'] && self['_rev']
self[self.model_type_key] = self.class.to_s
self[self.model_type_key] = self.class.model_type_string
end

yield self if block_given?
Expand All @@ -66,7 +70,7 @@ def initialize(attributes = {}, options = {})
alias :new_record? :new?
alias :new_document? :new?

# Compare this model with another by confirming to see
# Compare this model with another by confirming to see
# if the IDs and their databases match!
#
# Camparison of the database is required in case the
Expand Down
2 changes: 2 additions & 0 deletions lib/couchrest/model/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Configuration

included do
add_config :model_type_key
add_config :model_type_modification
add_config :mass_assign_any_attribute
add_config :auto_update_design_doc
add_config :environment
Expand All @@ -18,6 +19,7 @@ module Configuration

configure do |config|
config.model_type_key = 'type' # was 'couchrest-type'
config.model_type_modification = :to_s
config.mass_assign_any_attribute = false
config.auto_update_design_doc = true
config.time_fraction_digits = 3
Expand Down
4 changes: 2 additions & 2 deletions lib/couchrest/model/designs/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def define(design_doc, name, opts = {})
if name.to_s == 'all'
opts[:map] = <<-EOF
function(doc) {
if (doc['#{model.model_type_key}'] == '#{model.to_s}') {
if (doc['#{model.model_type_key}'] == '#{model.model_type_string}') {
emit(doc._id, null);
}
}
Expand All @@ -473,7 +473,7 @@ def define(design_doc, name, opts = {})

opts[:allow_blank] = opts[:allow_blank].nil? ? true : opts[:allow_blank]
opts[:guards] ||= []
opts[:guards].push "(doc['#{model.model_type_key}'] == '#{model.to_s}')"
opts[:guards].push "(doc['#{model.model_type_key}'] == '#{model.model_type_string}')"

keys = opts[:by].map{|o| "doc['#{o}']"}
emit = keys.length == 1 ? keys.first : "[#{keys.join(', ')}]"
Expand Down
2 changes: 1 addition & 1 deletion lib/couchrest/model/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ module ClassMethods
#
def build_from_database(doc = {}, options = {}, &block)
src = doc[model_type_key]
base = (src.blank? || src == self.to_s) ? self : src.constantize
base = (src.blank? || src == self.to_s) ? self : src.classify.constantize
base.new(doc, options.merge(:directly_set_attributes => true), &block)
end

Expand Down
4 changes: 4 additions & 0 deletions spec/unit/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
@doc[WithDefaultValues.model_type_key].should eql('WithDefaultValues')
end

it "should return the model type string", focus: true do
WithDefaultValues.model_type_string.should eql('WithDefaultValues')
end

it "should call after_initialize method if available" do
@doc = WithAfterInitializeMethod.new
@doc['some_value'].should eql('value')
Expand Down
18 changes: 13 additions & 5 deletions spec/unit/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
end

describe '.add_config' do

it "should add a class level accessor" do
@class.add_config :foo_bar
@class.foo_bar = 'foo'
@class.foo_bar.should == 'foo'
end

['foo', :foo, 45, ['foo', :bar]].each do |val|
it "should be inheritable for a #{val.class}" do
@class.add_config :foo_bar
Expand All @@ -40,14 +40,14 @@
@class.foo_bar.should == val
end
end


it "should add an instance level accessor" do
@class.add_config :foo_bar
@class.foo_bar = 'foo'
@class.new.foo_bar.should == 'foo'
end

it "should add a convenient in-class setter" do
@class.add_config :foo_bar
@class.foo_bar "monkey"
Expand All @@ -74,4 +74,12 @@
end
end

describe "model type key modification" do
it "should modify model type", :focus => true do
CouchRest::Model::Base.configure do |config|
config.model_type_modification = :downcase
end
Cat.new[:type].should == 'cat'
end
end
end
4 changes: 4 additions & 0 deletions spec/unit/persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
doc.class.should eql(WithTemplateAndUniqueID)
end

it "should instantialize with classify", focus: true do
doc = Article.build_from_database({'_id' => 'testitem2', '_rev' => 123, 'type' => 'article', 'name' => 'my test'})
doc.class.should eql(Article)
end
end

describe "basic saving and retrieving" do
Expand Down