1+ # frozen_string_literal: true
12require 'active_support'
23require 'active_support/core_ext/object/to_param'
34require 'active_model'
45
56module RSpec ::ActiveModel ::Mocks
67 class IllegalDataAccessException < StandardError ; end
78 module Mocks
8-
99 module ActiveModelInstanceMethods
1010 # Stubs `persisted?` to return false and `id` to return nil
1111 # @return self
@@ -91,12 +91,13 @@ def association(association_name)
9191 # * A String representing a Class that does not exist
9292 # * A String representing a Class that extends ActiveModel::Naming
9393 # * A Class that extends ActiveModel::Naming
94- def mock_model ( string_or_model_class , stubs = { } )
94+ def mock_model ( string_or_model_class , stubs = { } )
9595 if String === string_or_model_class
9696 if Object . const_defined? ( string_or_model_class )
9797 model_class = Object . const_get ( string_or_model_class )
9898 else
9999 model_class = Object . const_set ( string_or_model_class , Class . new do
100+ # rubocop:disable Style/SingleLineMethods
100101 extend ::ActiveModel ::Naming
101102 def self . primary_key ; :id ; end
102103
@@ -106,32 +107,33 @@ def self._reflect_on_association(_other); nil; end
106107 def self . composite_primary_key? ; false ; end
107108 def self . has_query_constraints? ; false ; end
108109 def self . param_delimiter ; "-" ; end
110+ # rubocop:enable Style/SingleLineMethods
109111 end )
110112 end
111113 else
112114 model_class = string_or_model_class
113115 end
114116
115117 unless model_class . kind_of? ::ActiveModel ::Naming
116- raise ArgumentError . new <<-EOM
118+ raise ArgumentError , <<-EOM
117119The mock_model method can only accept as its first argument:
118120* A String representing a Class that does not exist
119121* A String representing a Class that extends ActiveModel::Naming
120122* A Class that extends ActiveModel::Naming
121123
122124It received #{ model_class . inspect }
123- EOM
125+ EOM
124126 end
125127
126- stubs = { :id => next_id } . merge ( stubs )
127- stubs = { :persisted? => !!stubs [ :id ] ,
128- :destroyed? => false ,
129- :marked_for_destruction? => false ,
130- :valid? => true ,
131- :blank? => false } . merge ( stubs )
128+ stubs = { :id => next_id } . merge ( stubs )
129+ stubs = { :persisted? => !!stubs [ :id ] ,
130+ :destroyed? => false ,
131+ :marked_for_destruction? => false ,
132+ :valid? => true ,
133+ :blank? => false } . merge ( stubs )
132134
133135 double ( "#{ model_class . name } _#{ stubs [ :id ] } " , stubs ) . tap do |m |
134- if model_class . method ( :=== ) . owner == Module && !stubs . has_key ?( :=== )
136+ if model_class . method ( :=== ) . owner == Module && !stubs . key ?( :=== )
135137 allow ( model_class ) . to receive ( :=== ) . and_wrap_original do |original , other |
136138 m === other || original . call ( other )
137139 end
@@ -143,50 +145,52 @@ def self.param_delimiter; "-"; end
143145 include ActiveModel ::Conversion
144146 include ActiveModel ::Validations
145147 end
146- if defined? ( ActiveRecord )
147- if stubs . values_at ( :save , :update_attributes , :update ) . include? ( false )
148- RSpec ::Mocks . allow_message ( m . errors , :empty? ) . and_return ( false )
149- RSpec ::Mocks . allow_message ( m . errors , :blank? ) . and_return ( false )
150- end
148+ if defined? ( ActiveRecord ) && stubs . values_at ( :save , :update_attributes , :update ) . include? ( false )
149+ RSpec ::Mocks . allow_message ( m . errors , :empty? ) . and_return ( false )
150+ RSpec ::Mocks . allow_message ( m . errors , :blank? ) . and_return ( false )
151151 end
152152
153153 msingleton . __send__ ( :define_method , :is_a? ) do |other |
154154 model_class . ancestors . include? ( other )
155- end unless stubs . has_key ?( :is_a? )
155+ end unless stubs . key ?( :is_a? )
156156
157157 msingleton . __send__ ( :define_method , :kind_of? ) do |other |
158158 model_class . ancestors . include? ( other )
159- end unless stubs . has_key ?( :kind_of? )
159+ end unless stubs . key ?( :kind_of? )
160160
161161 msingleton . __send__ ( :define_method , :instance_of? ) do |other |
162162 other == model_class
163- end unless stubs . has_key ?( :instance_of? )
163+ end unless stubs . key ?( :instance_of? )
164164
165165 msingleton . __send__ ( :define_method , :__model_class_has_column? ) do |method_name |
166166 model_class . respond_to? ( :column_names ) && model_class . column_names . include? ( method_name . to_s )
167167 end
168168
169169 msingleton . __send__ ( :define_method , :has_attribute? ) do |attr_name |
170170 __model_class_has_column? ( attr_name )
171- end unless stubs . has_key ?( :has_attribute? )
171+ end unless stubs . key ?( :has_attribute? )
172172
173173 msingleton . __send__ ( :define_method , :respond_to? ) do |method_name , *args |
174- include_private = args . first || false
174+ include_private = args . first || false
175175 __model_class_has_column? ( method_name ) ? true : super ( method_name , include_private )
176- end unless stubs . has_key ?( :respond_to? )
176+ end unless stubs . key ?( :respond_to? )
177177
178- msingleton . __send__ ( :define_method , :method_missing ) do |m , *a , &b |
179- respond_to? ( m ) ? null_object? ? self : nil : super ( m , *a , &b )
178+ msingleton . __send__ ( :define_method , :method_missing ) do |missing_m , *a , &b |
179+ if respond_to? ( missing_m )
180+ null_object? ? self : nil
181+ else
182+ super ( missing_m , *a , &b )
183+ end
180184 end
181185
182186 msingleton . __send__ ( :define_method , :class ) do
183187 model_class
184- end unless stubs . has_key ?( :class )
188+ end unless stubs . key ?( :class )
185189
186190 mock_param = to_param
187191 msingleton . __send__ ( :define_method , :to_s ) do
188192 "#{ model_class . name } _#{ mock_param } "
189- end unless stubs . has_key ?( :to_s )
193+ end unless stubs . key ?( :to_s )
190194 yield m if block_given?
191195 end
192196 end
@@ -208,7 +212,7 @@ def persisted?
208212 module ActiveRecordStubExtensions
209213 # Stubs `id` (or other primary key method) to return nil
210214 def as_new_record
211- self . __send__ ( "#{ self . class . primary_key } =" , nil )
215+ __send__ ( "#{ self . class . primary_key } =" , nil )
212216 super
213217 end
214218
@@ -220,7 +224,8 @@ def new_record?
220224 # Raises an IllegalDataAccessException (stubbed models are not allowed to access the database)
221225 # @raises IllegalDataAccessException
222226 def connection
223- raise RSpec ::ActiveModel ::Mocks ::IllegalDataAccessException . new ( "stubbed models are not allowed to access the database" )
227+ raise RSpec ::ActiveModel ::Mocks ::IllegalDataAccessException ,
228+ "stubbed models are not allowed to access the database"
224229 end
225230 end
226231
@@ -257,13 +262,13 @@ def stub_model(model_class, stubs={})
257262 if defined? ( ActiveRecord ) && model_class < ActiveRecord ::Base && model_class . primary_key
258263 m . extend ActiveRecordStubExtensions
259264 primary_key = model_class . primary_key . to_sym
260- stubs = { primary_key => next_id } . merge ( stubs )
261- stubs = { :persisted? => !!stubs [ primary_key ] } . merge ( stubs )
265+ stubs = { primary_key => next_id } . merge ( stubs )
266+ stubs = { :persisted? => !!stubs [ primary_key ] } . merge ( stubs )
262267 else
263- stubs = { :id => next_id } . merge ( stubs )
264- stubs = { :persisted? => !!stubs [ :id ] } . merge ( stubs )
268+ stubs = { :id => next_id } . merge ( stubs )
269+ stubs = { :persisted? => !!stubs [ :id ] } . merge ( stubs )
265270 end
266- stubs = { :blank? => false } . merge ( stubs )
271+ stubs = { :blank? => false } . merge ( stubs )
267272
268273 stubs . each do |message , return_value |
269274 if m . respond_to? ( "#{ message } =" )
@@ -281,14 +286,13 @@ def stub_model(model_class, stubs={})
281286 end
282287 end
283288
284- private
289+ private
285290
286291 @@model_id = 1000
287292
288293 def next_id
289294 @@model_id += 1
290295 end
291-
292296 end
293297end
294298
0 commit comments