Last active
December 25, 2015 11:59
-
-
Save mackato/6972768 to your computer and use it in GitHub Desktop.
Rails 4 Postgresql store accessor matcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Shoulda | |
module Matchers | |
module ActiveRecord | |
def have_store_accessor(key) | |
HaveStoreAccessor.new(key) | |
end | |
class HaveStoreAccessor | |
def initialize(key) | |
@key = key | |
@options = {} | |
end | |
def within(store_attribute) | |
@options[:store_attribute] = store_attribute | |
self | |
end | |
def matches?(subject) | |
@subject = subject | |
key_accessor_exists? && correct_store? | |
end | |
def description | |
desc = "have store accessor named #{@key}" | |
desc << " within the #{@options[:store_attribute]}" if @options.key?(:store_attribute) | |
end | |
def failure_message_for_should | |
"Expected #{expectation} (#{@missing})" | |
end | |
def failure_message_for_should_not | |
"Did not expect #{expectation}" | |
end | |
protected | |
def key_accessor_exists? | |
if @subject.respond_to?(@key) && @subject.respond_to?("#{@key}=") | |
true | |
else | |
@missing = "#{@subject.class} does not have a store accessor named #{@key}" | |
end | |
end | |
def correct_store? | |
return true unless @options.key?(:store_attribute) | |
@subject.send(:"#{@key}=", '1') | |
val = @subject.send(@key) | |
val == '1' && @subject.send(@options[:store_attribute])[@key] == val | |
rescue | |
false | |
end | |
def expectation | |
expected = "#{@subject.class} to #{description}" | |
end | |
end | |
end | |
end | |
end |
Maybe I'm missing something but key_accessor_exists? will always return true...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm getting a deprecation warning from RSpec 3.2.0. To remove I've just renamed the methods
failure_message_for_should
andfailure_message_for_should_not
intofailure_message
andfailure_message_when_negated
respectively. Btw, thanks a lot for this gist, great job!