Permalink
Browse files

initial

  • Loading branch information...
0 parents commit 51a02c9ee3ea9a8ace37b6ad4eaa2aa9df0d261c @grosser committed Oct 3, 2008
Showing with 114 additions and 0 deletions.
  1. +24 −0 README
  2. +3 −0 init.rb
  3. +27 −0 install.rb
  4. +60 −0 lib/valid_attributes.rb
24 README
@@ -0,0 +1,24 @@
+#GOALS
+ - create single valid record
+ - edge-case records without fixtures
+ - valid attributes to test a post or fill a form
+
+
+#INSTALL
+Fill the test/fixtures/valid/valid.yml with 1 valid set of attributes per model
+Example:
+user:
+ name: Hans
+ login: hand
+ address_id: 1
+
+
+#USAGE
+ - set of valid attributes: valid_attributes User
+ - a valid Record(new): valid User
+ - a valid saved Record: create_valid User
+ - an edge-case User: valid User, :name=>'oh noo it is too long'
+
+
+#CONTRIBUTORS
+assert_invalid: http://www.railsforum.com/viewtopic.php?id=741
@@ -0,0 +1,3 @@
+if RAILS_ENV == 'test'
+ require File.join(File.dirname(__FILE__),'lib','valid_attributes')
+end
@@ -0,0 +1,27 @@
+def recursive_mk_dir(path)
+ require 'fileutils'
+ FileUtils.mkdir_p(path)
+end
+
+#load Grosser::Valid
+#make the normal init.rb useable
+here = __FILE__
+4.times{here = File.dirname(here)}
+RAILS_ROOT = here
+RAILS_ENV='test'
+require File.join(File.dirname(__FILE__),'init')
+
+path = Grosser::Valid.path_to_valid_yml
+unless File.exist?(path)
+ recursive_mk_dir File.dirname(path)
+ File.open(path,'w') do |f|
+ f.puts 'User'
+ f.puts ' name: Hans'
+ f.puts ' login: hans'
+ f.puts ' fill_out: the rest...'
+ end
+
+ puts "Created: #{path}"
+else
+ puts "Exists: #{path}"
+end
@@ -0,0 +1,60 @@
+#create a valid new record
+#attributes are set separetly(no mass-assignment)
+#valid User or valid 'user' or valid User, :name=>'Peter'
+def valid(type,attributes={})
+ record = type.to_s.camelcase.constantize.new
+ valid_attributes(type.to_s.underscore).merge(attributes).each do |k,v|
+ record.send("#{k}=",v)
+ end
+ record
+end
+
+def create_valid(type,attributes={})
+ record = valid(type,attributes)
+ record.save!
+ record
+end
+
+def valid_attributes(record_name,attributes={})
+ Grosser::Valid.load_valid_attributes(record_name).merge(attributes.to_options)
+end
+
+#example: User, :login=>['',nil,'admin'], :email=>['',nil,'aa','@','a@','@a']
+def assert_invalid_attributes(model_class, attributes)
+ attributes.each_pair do |attribute, value|
+ assert_invalid_value model_class, attribute, value
+ end
+end
+
+#example: User, :login, ['',nil,'admin']
+def assert_invalid_value(model_class, attribute, value)
+ if value.kind_of? Array
+ value.each { |v| assert_invalid_value model_class, attribute, v }
+ else
+ record = valid model_class
+ record.send(attribute.to_s+'=',value)
+ assert_block "<#{model_class}.#{attribute}> expected to be invalid when set to <#{value}>" do
+ record.valid? # Must be called to generate the errors
+ record.errors.invalid? attribute
+ end
+ end
+end
+
+module Grosser
+ class Valid
+ def self.load_valid_attributes(record_name)
+ name = record_name.to_s.underscore
+ @@valid_attributes ||= YAML::load_file(path_to_valid_yml)
+ begin
+ attrs = @@valid_attributes[name].to_options!
+ rescue
+ raise "attributes for #{name} not found! --> valid.yml"
+ end
+ attrs
+ end
+
+ def self.path_to_valid_yml
+ File.join(RAILS_ROOT,'test','fixtures','valid','valid.yml')
+ end
+ end
+end

0 comments on commit 51a02c9

Please sign in to comment.