Module: Iry

Defined in:
lib/iry.rb,
lib/iry/patch.rb,
lib/iry/macros.rb,
lib/iry/version.rb,
lib/iry/handlers.rb,
lib/iry/callbacks.rb,
lib/iry/constraint.rb,
lib/iry/handlers/pg.rb,
lib/iry/handlers/null.rb,
lib/iry/constraint/check.rb,
lib/iry/constraint/unique.rb,
lib/iry/constraint/exclusion.rb,
lib/iry/transform_constraints.rb,
lib/iry/constraint/foreign_key.rb

Overview

Entrypoint of constraint validation, include in a class inheriting ActiveRecord::Base and the following class-level methods will be available:

Examples:

User unique constraint validation

# The database schema has a unique constraint on email field
class User < ActiveRecord::Base
  include Iry

  unique_constraint :email
end

user = User.create!(email: "[email protected]")
fail_user = User.new(email: "[email protected]")
Iry.save(fail_user)
fail_user.errors.details.fetch(:email) #=> [{error: :taken}]

Defined Under Namespace

Modules: Constraint, Error, Handlers, Macros, Patch, TransformConstraints Classes: ConstraintViolation, StatementInvalid

Constant Summary collapse

VERSION =

Returns:

  • (String)
File.read(File.expand_path("../../VERSION", __dir__)).strip.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.destroy(model) ⇒ Handlers::Model

Similar to ActiveRecord::Base#destroy but in case of constraint violations, ‘false` is returned and `errors` are populated.

Parameters:

Returns:



153
154
155
# File 'lib/iry.rb', line 153

def self.destroy(model)
  TransformConstraints.destroy(model)
end

.handle_constraints(model) { ... } ⇒ nil, Handlers::Model

Executes block and in case of constraints violations on ‘model`, block is halted and errors are appended to `model`

Examples:

Handle constraints for unique user

# The database schema has a unique constraint on email field
class User < ActiveRecord::Base
  include Iry

  unique_constraint :email
end

user = User.create!(email: "[email protected]")
fail_user = User.new(email: "[email protected]")
result = Iry.handle_constraints(fail_user) { fail_user.save }
result #=> nil
fail_user.errors.details.fetch(:email) #=> [{error: :taken}]

Parameters:

  • model (Handlers::Model)

    model object for which constraints should be monitored and for which errors should be added to

Yields:

  • block must perform the save operation, usually with ‘save`

Returns:

  • (nil, Handlers::Model)

    the ‘model` or `nil` if a a constraint is violated



110
111
112
# File 'lib/iry.rb', line 110

def self.handle_constraints(model, &block)
  TransformConstraints.handle_constraints(model, &block)
end

.handle_constraints!(model) { ... } ⇒ Handlers::Model

Executes block and in case of constraints violations on ‘model`, block is halted, errors are appended to `model` and StatementInvalid is raised

Parameters:

  • model (Handlers::Model)

    model object for which constraints should be monitored and for which errors should be added to

Yields:

  • block must perform the save operation, usually with ‘save`

Returns:



120
121
122
# File 'lib/iry.rb', line 120

def self.handle_constraints!(model, &block)
  TransformConstraints.handle_constraints!(model, &block)
end

.save(model) ⇒ Boolean

Similar to ActiveRecord::Base#save but in case of constraint violations, ‘false` is returned and `errors` are populated. Aside from `model`, it takes the same arguments as ActiveRecord::Base#save

Parameters:

Returns:

  • (Boolean)

    ‘true` if successful



130
131
132
# File 'lib/iry.rb', line 130

def self.save(model, ...)
  TransformConstraints.save(model, ...)
end

.save!(model) ⇒ true

Similar to ActiveRecord::Base#save! but in case of constraint violations, it raises ConstraintViolation and ‘errors` are populated. Aside from `model`, it takes the same arguments as ActiveRecord::Base#save!

Parameters:

Returns:

  • (true)

Raises:

  • (ConstraintViolation)

    ConstraintViolation inherits from ActiveRecord::RecordInvalid but it’s triggered only when a constraint violation happens

  • (ActiveRecord::RecordInvalid)

    triggered when a validation error is raised, but not a constraint violation



145
146
147
# File 'lib/iry.rb', line 145

def self.save!(model, ...)
  TransformConstraints.save!(model, ...)
end

Instance Method Details

#recordHandlers::Model

Inherited from ActiveRecord::RecordInvalid, returns the model for which the constraint violations have been detected

Returns:



# File 'lib/iry.rb', line 50