diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8015a65..be2bcca 100755 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -47,14 +47,6 @@ RSpec/IteratedExpectation: RSpec/MultipleExpectations: Max: 6 -# Offense count: 2 -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: nested, compact -Style/ClassAndModuleChildren: - Exclude: - - 'lib/machinist/active_record/blueprint.rb' - - 'lib/machinist/active_record/lathe.rb' - # Offense count: 1 # Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms. # AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS diff --git a/lib/machinist/active_record/blueprint.rb b/lib/machinist/active_record/blueprint.rb index 35616be..406812b 100755 --- a/lib/machinist/active_record/blueprint.rb +++ b/lib/machinist/active_record/blueprint.rb @@ -1,14 +1,19 @@ -module Machinist::ActiveRecord - class Blueprint < Machinist::Blueprint - # Make and save an object. - def make!(attributes = {}) - object = make(attributes) - object.save! - object.reload - end +module Machinist + module ActiveRecord + class Blueprint < Machinist::Blueprint + # Make and save an object. + def make!(attributes = {}) + object = make(attributes) + object.save! + object.reload + rescue ::ActiveRecord::RecordInvalid => exception + message = "#{exception.class.name}: \"#{exception.message}\" saving #{exception.record.class.name}" + raise Machinist::BlueprintCantSaveError.new(self, message) + end - def lathe_class #:nodoc: - Machinist::ActiveRecord::Lathe + def lathe_class #:nodoc: + Machinist::ActiveRecord::Lathe + end end end end diff --git a/lib/machinist/active_record/lathe.rb b/lib/machinist/active_record/lathe.rb old mode 100644 new mode 100755 index d5e8f0e..4839808 --- a/lib/machinist/active_record/lathe.rb +++ b/lib/machinist/active_record/lathe.rb @@ -1,20 +1,22 @@ -module Machinist::ActiveRecord - class Lathe < Machinist::Lathe - def make_one_value(attribute, args) #:nodoc: - if block_given? - raise_argument_error(attribute) unless args.empty? - yield - else - make_association(attribute, args) +module Machinist + module ActiveRecord + class Lathe < Machinist::Lathe + def make_one_value(attribute, args) #:nodoc: + if block_given? + raise_argument_error(attribute) unless args.empty? + yield + else + make_association(attribute, args) + end end - end - def make_association(attribute, args) #:nodoc: - association = @klass.reflect_on_association(attribute) - if association - association.klass.make(*args) - else - raise_argument_error(attribute) + def make_association(attribute, args) #:nodoc: + association = @klass.reflect_on_association(attribute) + if association + association.klass.make(*args) + else + raise_argument_error(attribute) + end end end end diff --git a/lib/machinist/exceptions.rb b/lib/machinist/exceptions.rb old mode 100644 new mode 100755 index 6dc51b3..c236dfe --- a/lib/machinist/exceptions.rb +++ b/lib/machinist/exceptions.rb @@ -4,12 +4,13 @@ module Machinist class BlueprintCantSaveError < RuntimeError attr_reader :blueprint - def initialize(blueprint) + def initialize(blueprint, message = nil) @blueprint = blueprint + @message = message end def message - "make! is not supported by blueprints for class #{@blueprint.klass.name}" + @message ||= "make! is not supported by blueprints for class #{@blueprint.klass.name}" end end diff --git a/lib/machinist/version.rb b/lib/machinist/version.rb index 125306b..4eae91e 100755 --- a/lib/machinist/version.rb +++ b/lib/machinist/version.rb @@ -1,3 +1,3 @@ module Machinist - VERSION = '3.0.0'.freeze + VERSION = '3.0.2'.freeze end