Skip to content

Commit

Permalink
Merge pull request #55 from GetOccasion/thread_safety
Browse files Browse the repository at this point in the history
Fix Use ActiveSupport::CurrentAttributes instead of class state info to make it work with Rails (on Puma)
  • Loading branch information
kieranklaassen committed Feb 5, 2020
2 parents 3f406ce + 09a17e4 commit 0d04ed0
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion caprese.gemspec
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'active_model_serializers', '0.10.7'
spec.add_dependency 'kaminari', '>= 0.17.0'
spec.add_dependency 'rails', '>= 5.1.0', '< 6.0.0'
spec.add_dependency 'rails', '>= 5.2.0', '< 6.0.0'

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'factory_girl'
Expand Down
1 change: 1 addition & 0 deletions lib/caprese.rb
Expand Up @@ -3,6 +3,7 @@
require 'caprese/routing/caprese_resources'
require 'caprese/serializer'
require 'caprese/version'
require 'caprese/app/models/current'

module Caprese
def self.config
Expand Down
5 changes: 5 additions & 0 deletions lib/caprese/app/models/current.rb
@@ -0,0 +1,5 @@
module Caprese
class Current < ActiveSupport::CurrentAttributes
attribute :caprese_style_errors
end
end
7 changes: 2 additions & 5 deletions lib/caprese/controller/concerns/errors.rb
Expand Up @@ -7,7 +7,7 @@ module Errors
extend ActiveSupport::Concern

included do
around_action :enable_caprese_style_errors
before_action :enable_caprese_style_errors
rescue_from(StandardError) { |e| handle_exception(e) }
end

Expand All @@ -31,15 +31,12 @@ def error(field: nil, code: :invalid, t: {})

# Temporarily render model errors as Caprese::Record::Errors instead of ActiveModel::Errors
def enable_caprese_style_errors
Caprese::Record.caprese_style_errors = true
yield
Caprese::Record.caprese_style_errors = false
Current.caprese_style_errors = true
end

# Gracefully handles exceptions raised during Caprese controller actions.
# Override this method in your controller to add your own exception handlers
def handle_exception(e)
Caprese::Record.caprese_style_errors = false
if e.is_a?(Caprese::Error)
output = { json: e }
render output.merge(e.header)
Expand Down
5 changes: 1 addition & 4 deletions lib/caprese/record.rb
Expand Up @@ -11,12 +11,9 @@ module Record
extend ActiveSupport::Concern
include Aliasing

mattr_accessor :caprese_style_errors
@@caprese_style_errors = false

# @return [Errors] a cached instance of the model errors class
def errors
@errors ||= (Caprese::Record.caprese_style_errors ? Caprese::Record : ActiveModel)::Errors.new(self)
@errors ||= (Current.caprese_style_errors ? Caprese::Record : ActiveModel)::Errors.new(self)
end
end
end
2 changes: 1 addition & 1 deletion lib/caprese/record/associated_validator.rb
Expand Up @@ -15,7 +15,7 @@ module Record
# ]
class AssociatedValidator < ActiveRecord::Validations::AssociatedValidator
def validate_each(record, attribute, value)
if Caprese::Record.caprese_style_errors
if Current.caprese_style_errors
Array(value).reject { |r| r.marked_for_destruction? || r.valid? }.each do |invalid_record|
invalid_record.errors.to_a.each do |error|
field_name = error.field ? "#{attribute}.#{error.field}" : attribute
Expand Down
4 changes: 2 additions & 2 deletions spec/record_spec.rb
Expand Up @@ -3,8 +3,8 @@
describe Caprese::Record, type: :model do
let(:record) { create :post }

before { Caprese::Record.caprese_style_errors = true }
after { Caprese::Record.caprese_style_errors = false }
before { Caprese::Current.caprese_style_errors = true }
after { Caprese::Current.caprese_style_errors = false }

describe '#errors' do
let(:options) { {} }
Expand Down

0 comments on commit 0d04ed0

Please sign in to comment.