Module: CommonLib::ActiveSupportExtension::Attributes::ClassMethods
- Defined in:
- lib/common_lib/active_support_extension/attributes.rb
Overview
def self.included
Instance Method Summary collapse
-
#assert_should_not_protect_attribute(*attributes) ⇒ Object
>> Abstract.protected_attributes => #<ActiveModel::MassAssignmentSecurity::BlackList: “entry_2_by_uid”, “entry_1_by_uid”, “id”, “type”, “study_subject_id”, “merged_by_uid”>.
- #assert_should_not_require_attribute(*attributes) ⇒ Object
- #assert_should_not_require_unique_attribute(*attributes) ⇒ Object
- #assert_should_protect_attribute(*attributes) ⇒ Object
- #assert_should_require_attribute(*attributes) ⇒ Object
- #assert_should_require_attribute_length(*attributes) ⇒ Object
- #assert_should_require_attribute_not_nil(*attributes) ⇒ Object
- #assert_should_require_unique_attribute(*attributes) ⇒ Object
Instance Method Details
#assert_should_not_protect_attribute(*attributes) ⇒ Object
>> Abstract.protected_attributes
> #<ActiveModel::MassAssignmentSecurity::BlackList: “entry_2_by_uid”, “entry_1_by_uid”, “id”, “type”, “study_subject_id”, “merged_by_uid”>
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/common_lib/active_support_extension/attributes.rb', line 272 def assert_should_not_protect_attribute(*attributes) = attributes. model = [:model] || model_name_without_test attributes.flatten.each do |attr| attr = attr.to_s test "#{brand}should not protect attribute #{attr}" do assert !model.constantize.protected_attributes.include?(attr), "#{attr} is included in protected attributes" # Rails 3 change # apparently no longer always true # if !model.accessible_attributes.nil? # assert model.accessible_attributes.include?(attr), # "#{attr} is not included in accessible attributes" # end end end end |
#assert_should_not_require_attribute(*attributes) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/common_lib/active_support_extension/attributes.rb', line 132 def assert_should_not_require_attribute(*attributes) = attributes. model = [:model] || model_name_without_test attributes.flatten.each do |attr| attr = attr.to_s test "#{brand}should not require #{attr}" do object = model.constantize.new object.send("#{attr}=", nil) # don't know if it will be true or false, but must be called object.valid? assert !object.errors.include?(attr.to_sym), object.errors..to_sentence if attr =~ /^(.*)_id$/ assert !object.errors.include?($1.to_sym), object.errors..to_sentence end end end end |
#assert_should_not_require_unique_attribute(*attributes) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/common_lib/active_support_extension/attributes.rb', line 36 def assert_should_not_require_unique_attribute(*attributes) = attributes. attributes.flatten.each do |attr| attr = attr.to_s title = "#{brand}should not require unique #{attr}" scope = [:scope] unless scope.blank? title << " scope " title << (( scope.is_a?(Array) ) ? scope.join(',') : scope.to_s ) end test title do o = create_object attrs = { attr.to_sym => o.send(attr) } if( scope.is_a?(String) || scope.is_a?(Symbol) ) attrs[scope.to_sym] = o.send(scope.to_sym) elsif scope.is_a?(Array) scope.each do |s| attrs[s.to_sym] = o.send(s.to_sym) end end object = create_object(attrs) assert !object.errors.matching?(attr,'has already been taken'), object.errors..to_sentence end end end |
#assert_should_protect_attribute(*attributes) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/common_lib/active_support_extension/attributes.rb', line 247 def assert_should_protect_attribute(*attributes) = attributes. model = [:model] || model_name_without_test attributes.flatten.each do |attr| attr = attr.to_s test "#{brand}should protect attribute #{attr}" do assert model.constantize.accessible_attributes||model.constantize.protected_attributes, "Both accessible and protected attributes are empty" assert !(model.constantize.accessible_attributes||[]).include?(attr), "#{attr} is included in accessible attributes" if !model.constantize.protected_attributes.nil? assert model.constantize.protected_attributes.include?(attr), "#{attr} is not included in protected attributes" end end end end |
#assert_should_require_attribute(*attributes) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/common_lib/active_support_extension/attributes.rb', line 113 def assert_should_require_attribute(*attributes) = attributes. model = [:model] || model_name_without_test attributes.flatten.each do |attr| attr = attr.to_s test "#{brand}should require #{attr}" do object = model.constantize.new object.send("#{attr}=", nil) assert !object.valid? assert object.errors.include?(attr.to_sym), object.errors..to_sentence assert object.errors.matching?(attr,"can't be blank") || object.errors.matching?(attr,'is too short'), object.errors..to_sentence end end end |
#assert_should_require_attribute_length(*attributes) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/common_lib/active_support_extension/attributes.rb', line 153 def assert_should_require_attribute_length(*attributes) = attributes. model = [:model] || model_name_without_test if( ( .keys & [:in,:within] ).length >= 1 ) range = [:in]||[:within] [:minimum] = range.min [:maximum] = range.max end attributes.flatten.each do |attr| attr = attr.to_s if .keys.include?(:is) length = [:is] test "#{brand}should require exact length of #{length} for #{attr}" do value = 'x'*(length-1) object = model.constantize.new object.send("#{attr}=", value) assert !object.valid? assert_equal length-1, object.send(attr.to_sym).length assert_equal object.send(attr.to_sym), value assert object.errors.include?(attr.to_sym), object.errors..to_sentence assert object.errors.matching?(attr,'is the wrong length'), object.errors..to_sentence value = 'x'*(length+1) object = model.constantize.new object.send("#{attr}=", value) assert !object.valid? assert_equal length+1, object.send(attr.to_sym).length assert_equal object.send(attr.to_sym), value assert object.errors.include?(attr.to_sym), object.errors..to_sentence assert object.errors.matching?(attr,'is the wrong length'), object.errors..to_sentence end end if .keys.include?(:minimum) min = [:minimum] test "#{brand}should require min length of #{min} for #{attr}" do value = 'x'*(min) object = model.constantize.new object.send("#{attr}=", value) # don't know if really valid object.valid? assert_equal min, object.send(attr.to_sym).length assert_equal object.send(attr.to_sym), value assert !object.errors.matching?(attr,'is too short'), object.errors..to_sentence value = 'x'*(min-1) object = model.constantize.new object.send("#{attr}=", value) assert !object.valid? assert_equal min-1, object.send(attr.to_sym).length assert_equal object.send(attr.to_sym), value assert object.errors.include?(attr.to_sym), object.errors..to_sentence assert object.errors.matching?(attr,'is too short'), object.errors..to_sentence end end if .keys.include?(:maximum) max = [:maximum] test "#{brand}should require max length of #{max} for #{attr}" do value = 'x'*(max) object = model.constantize.new object.send("#{attr}=", value) # don't know if really valid object.valid? assert_equal max, object.send(attr.to_sym).length assert_equal object.send(attr.to_sym), value assert !object.errors.matching?(attr,'is too long'), object.errors..to_sentence value = 'x'*(max+1) object = model.constantize.new object.send("#{attr}=", value) assert !object.valid? assert_equal max+1, object.send(attr.to_sym).length assert_equal object.send(attr.to_sym), value assert object.errors.include?(attr.to_sym), object.errors..to_sentence assert object.errors.matching?(attr,'is too long'), object.errors..to_sentence end end end end |
#assert_should_require_attribute_not_nil(*attributes) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/common_lib/active_support_extension/attributes.rb', line 95 def assert_should_require_attribute_not_nil(*attributes) = attributes. model = [:model] || model_name_without_test attributes.flatten.each do |attr| attr = attr.to_s test "#{brand}should require #{attr} not nil" do object = model.constantize.new object.send("#{attr}=", nil) assert !object.valid? # message could be a number of things ... # "is not included in the list","can't be blank" assert object.errors.include?(attr.to_sym), object.errors..to_sentence end end end |
#assert_should_require_unique_attribute(*attributes) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/common_lib/active_support_extension/attributes.rb', line 64 def assert_should_require_unique_attribute(*attributes) = attributes. model = [:model] || model_name_without_test attributes.flatten.each do |attr| attr = attr.to_s title = "#{brand}should require unique #{attr}" scope = [:scope] unless scope.blank? title << " scope " title << (( scope.is_a?(Array) ) ? scope.join(',') : scope.to_s) end test title do o = create_object assert_no_difference "#{model}.count" do attrs = { attr.to_sym => o.send(attr) } if( scope.is_a?(String) || scope.is_a?(Symbol) ) attrs[scope.to_sym] = o.send(scope.to_sym) elsif scope.is_a?(Array) scope.each do |s| attrs[s.to_sym] = o.send(s.to_sym) end end object = create_object(attrs) assert object.errors.matching?(attr,'has already been taken'), object.errors..to_sentence end end end end |