Skip to content
Browse files

add validation and callback option support to #model

  • Loading branch information...
1 parent 1b0790e commit ece7088f027a3efdfc25e4555af133e1c2526e0e @technoweenie committed
Showing with 25 additions and 15 deletions.
  1. +5 −5 lib/model_stubbing.rb
  2. +10 −1 lib/model_stubbing/definition.rb
  3. +6 −5 lib/model_stubbing/model.rb
  4. +4 −4 lib/model_stubbing/stub.rb
View
10 lib/model_stubbing.rb
@@ -23,11 +23,11 @@ def self.records() @records ||= {} end
# configure this with the :validate or :callbacks options.
#
# Options:
- # * :copy - set to false if you don't want this definition to be a dup of
- # the :default definition
- # * :insert - set to false if you don't want to insert this definition
- # into the database.
- # * :validate - set to false if you don't want to validate model data, or run callbacks
+ # * :copy - set to false if you don't want this definition to be a dup of
+ # the :default definition
+ # * :insert - set to false if you don't want to insert this definition
+ # into the database.
+ # * :validate - set to false if you don't want to validate model data, or run callbacks
# * :callbacks - set to true if you want to run callbacks.
def define_models(name = nil, options = {}, &block)
if name.is_a? Hash
View
11 lib/model_stubbing/definition.rb
@@ -17,8 +17,16 @@ def current_time
# Creates a new ModelStubbing::Model to hold one or more stubs. Multiple calls will append
# any added stubs to the same model instance.
+ #
+ # Options:
+ # * :name - The name used of the model. Defaults to the "Foo".underscore.pluralize
+ # * :plural - The name of the method used to access the stubs in your test.
+ # Defaults to #name.
+ # * :singular - The name of the method for the new_* stub accessors.
+ # * :validate - set to false if you don't want to validate model data, or run callbacks
+ # * :callbacks - set to true if you want to run callbacks.
def model(klass, options = {}, &block)
- m = Model.new(self, klass, options)
+ m = Model.new(self, klass, @options.merge(options))
@ordered_models << m unless @models.key?(m.name)
@models[m.name] ||= m
@models[m.name].instance_eval(&block) if block
@@ -35,6 +43,7 @@ def initialize(&block)
def dup
copy = self.class.new
copy.current_time = @current_time
+ copy.options.update @options
models.each do |name, model|
copy.models[name] = model.dup(copy)
end
View
11 lib/model_stubbing/model.rb
@@ -2,7 +2,7 @@ module ModelStubbing
# Models hold one or more stubs.
class Model
attr_accessor :name, :plural, :singular
- attr_reader :definition, :stubs, :model_class
+ attr_reader :definition, :stubs, :model_class, :options
# Creates a stub for this model. A stub with no name is assumed to be the default
# stub. A global key for the definition is also created based on the singular
@@ -19,9 +19,10 @@ def stub(name = nil, options = {})
def initialize(definition, klass, options = {}, &block)
@definition = definition
@model_class = klass
- @name = options[:name] || default_name.to_sym
- @plural = options[:plural] || name
- @singular = options[:singular] || name.to_s.singularize
+ @name = options.delete(:name) || default_name.to_sym
+ @plural = options.delete(:plural) || name
+ @singular = options.delete(:singular) || name.to_s.singularize
+ @options = {}
@stubs = {}
unless @model_class.respond_to?(:mock_id)
class << @model_class
@@ -44,7 +45,7 @@ def default_name
end
def dup(definition = nil)
- copy = self.class.new(definition || @definition, @model_class, :name => @name, :plural => @plural, :singular => @singular)
+ copy = self.class.new(definition || @definition, @model_class, @options.merge(:name => @name, :plural => @plural, :singular => @singular))
stubs.each do |key, value|
copy.stubs[key] = value.dup(copy)
end
View
8 lib/model_stubbing/stub.rb
@@ -4,7 +4,7 @@ module ModelStubbing
# will return the exact same instance. However, custom attributes
# will create unique stub instances.
class Stub
- attr_reader :model, :attributes, :global_key, :name
+ attr_reader :model, :attributes, :global_key, :name
# Creates a new stub. If it's not the default, it inherits the default
# stub's attributes.
@@ -49,13 +49,13 @@ def inspect
def insert(attributes = {})
@inserting = true
object = record(attributes)
- if model.definition.options[:callbacks]
+ if model.options[:callbacks]
object.new_record = true # record could have returned one from the cache
object.save!
- elsif !model.definition.options[:validate] || object.valid?
+ elsif !model.options[:validate] || object.valid?
connection.insert_fixture(object.stubbed_attributes, model.model_class.table_name)
else
- raise "Model data is not valid: #{object.errors.full_messages.to_sentence}"
+ raise "#{model.model_class}##{@name} data is not valid: #{object.errors.full_messages.to_sentence}"
end
@inserting = false
end

0 comments on commit ece7088

Please sign in to comment.
Something went wrong with that request. Please try again.