Module: RailsExtension::ActiveSupportExtension::Attributes::ClassMethods
- Defined in:
- lib/rails_extension/active_support_extension/attributes.rb
Overview
def self.included
Instance Method Summary collapse
- #assert_should_not_protect_attribute(*attributes) ⇒ Object
- #assert_should_not_require_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
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/rails_extension/active_support_extension/attributes.rb', line 213 def assert_should_not_protect_attribute(*attributes) = attributes. model_name = [:model] || st_model_name model = model_name.constantize attributes.each do |attr| attr = attr.to_s test "#{brand}should not protect attribute #{attr}" do assert !(model.protected_attributes||[]).include?(attr), "#{attr} is included in protected attributes" 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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rails_extension/active_support_extension/attributes.rb', line 96 def assert_should_not_require_attribute(*attributes) = attributes. model = [:model] || st_model_name attributes.each do |attr| attr = attr.to_s test "#{brand}should not require #{attr}" do assert_difference( "#{model}.count", 1 ) do object = create_object(attr.to_sym => nil) assert !object.errors.on(attr.to_sym) if attr =~ /^(.*)_id$/ assert !object.errors.on($1.to_sym) end end end end end |
#assert_should_protect_attribute(*attributes) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/rails_extension/active_support_extension/attributes.rb', line 193 def assert_should_protect_attribute(*attributes) = attributes. model_name = [:model] || st_model_name model = model_name.constantize attributes.each do |attr| attr = attr.to_s test "#{brand}should protect attribute #{attr}" do assert model.accessible_attributes||model.protected_attributes, "Both accessible and protected attributes are empty" assert !(model.accessible_attributes||[]).include?(attr), "#{attr} is included in accessible attributes" if !model.protected_attributes.nil? assert model.protected_attributes.include?(attr), "#{attr} is not included in protected attributes" end end end end |
#assert_should_require_attribute(*attributes) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rails_extension/active_support_extension/attributes.rb', line 80 def assert_should_require_attribute(*attributes) = attributes. model = [:model] || st_model_name attributes.each do |attr| attr = attr.to_s test "#{brand}should require #{attr}" do assert_no_difference "#{model}.count" do object = create_object(attr.to_sym => nil) assert object.errors.on_attr_and_type(attr.to_sym, :blank) || object.errors.on_attr_and_type(attr.to_sym, :too_short) end end end end |
#assert_should_require_attribute_length(*attributes) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 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 |
# File 'lib/rails_extension/active_support_extension/attributes.rb', line 114 def assert_should_require_attribute_length(*attributes) = attributes. model = [:model] || st_model_name if( ( .keys & [:in,:within] ).length >= 1 ) range = [:in]||[:within] [:minimum] = range.min [:maximum] = range.max end attributes.each do |attr| attr = attr.to_s if .keys.include?(:is) length = [:is] test "#{brand}should require exact length of #{length} for #{attr}" do assert_no_difference "#{model}.count" do value = 'x'*(length-1) object = create_object(attr.to_sym => value) assert_equal length-1, object.send(attr.to_sym).length assert_equal object.send(attr.to_sym), value assert object.errors.on_attr_and_type(attr.to_sym, :wrong_length) end assert_no_difference "#{model}.count" do value = 'x'*(length+1) object = create_object(attr.to_sym => value) assert_equal length+1, object.send(attr.to_sym).length assert_equal object.send(attr.to_sym), value assert object.errors.on_attr_and_type(attr.to_sym, :wrong_length) end end end if .keys.include?(:minimum) min = [:minimum] test "#{brand}should require min length of #{min} for #{attr}" do # because the model may have other requirements # just check to ensure that we don't get a :too_short error # assert_difference "#{model}.count" do value = 'x'*(min) object = create_object(attr.to_sym => value) assert_equal min, object.send(attr.to_sym).length assert_equal object.send(attr.to_sym), value assert !object.errors.on_attr_and_type(attr.to_sym, :too_short) # end assert_no_difference "#{model}.count" do value = 'x'*(min-1) object = create_object(attr.to_sym => value) assert_equal min-1, object.send(attr.to_sym).length assert_equal object.send(attr.to_sym), value assert object.errors.on_attr_and_type(attr.to_sym, :too_short) end end end if .keys.include?(:maximum) max = [:maximum] test "#{brand}should require max length of #{max} for #{attr}" do # because the model may have other requirements # just check to ensure that we don't get a :too_long error # assert_difference "#{model}.count" do value = 'x'*(max) object = create_object(attr.to_sym => value) assert_equal max, object.send(attr.to_sym).length assert_equal object.send(attr.to_sym), value assert !object.errors.on_attr_and_type(attr.to_sym, :too_long) # end assert_no_difference "#{model}.count" do value = 'x'*(max+1) object = create_object(attr.to_sym => value) assert_equal max+1, object.send(attr.to_sym).length assert_equal object.send(attr.to_sym), value assert object.errors.on_attr_and_type(attr.to_sym, :too_long) end end end end end |
#assert_should_require_attribute_not_nil(*attributes) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rails_extension/active_support_extension/attributes.rb', line 65 def assert_should_require_attribute_not_nil(*attributes) = attributes. model = [:model] || st_model_name attributes.each do |attr| attr = attr.to_s test "#{brand}should require #{attr} not nil" do assert_no_difference "#{model}.count" do object = create_object(attr.to_sym => nil) assert object.errors.on(attr.to_sym) end end end end |
#assert_should_require_unique_attribute(*attributes) ⇒ Object
35 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 63 |
# File 'lib/rails_extension/active_support_extension/attributes.rb', line 35 def assert_should_require_unique_attribute(*attributes) = attributes. model = [:model] || st_model_name attributes.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.on_attr_and_type(attr.to_sym, :taken) end end end end |