story-helper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ === 0.1.0 / 2008-10-21
2
+
3
+ * Released version 0.1.0 on RubyForge today.
4
+
5
+ * Birthday!
6
+
@@ -0,0 +1,14 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/storify
6
+ lib/story-helper.rb
7
+ lib/story-helper/conf/story_accessors.rb
8
+ lib/story-helper/conf/story_helper.rake
9
+ lib/story-helper/conf/story_helper.rb
10
+ lib/story-helper/conf/test_helper_extensions.rb
11
+ lib/story-helper/version.rb
12
+ story-helper.gemspec
13
+ test/rails_version_test.rb
14
+ test/test_story_helper.rb
@@ -0,0 +1,53 @@
1
+ = story-helper
2
+
3
+ * http://github.com/jtrupiano/story-helper
4
+
5
+ == DESCRIPTION:
6
+
7
+ Provides very basic baseline for using direct Ruby to define your test data and test accessors. Still a work in progress,
8
+ but something we use at SLS in lieu of fixtures. More details to come as this becomes more useful to others.
9
+
10
+ There's some information on the wiki regarding how to use it in conjunction with fixtures.
11
+
12
+ == FEATURES/PROBLEMS:
13
+
14
+ * not entirely DRY (requires you to define your accessors in addition to your test data -- this should be able to be automated)
15
+ * could auto-convert existing fixtures
16
+ * should only be used in conjunction with pre-built test databases (in other words, no dropping and recreating for each test case -- use DB transactions instead)
17
+
18
+ == SYNOPSIS:
19
+
20
+ FIX (code sample of usage)
21
+
22
+ == REQUIREMENTS:
23
+
24
+ * FIX (list of requirements)
25
+
26
+ == INSTALL:
27
+
28
+ * sudo gem install story-helper
29
+
30
+ == LICENSE:
31
+
32
+ (The MIT License)
33
+
34
+ Copyright (c) 2008 FIX
35
+
36
+ Permission is hereby granted, free of charge, to any person obtaining
37
+ a copy of this software and associated documentation files (the
38
+ 'Software'), to deal in the Software without restriction, including
39
+ without limitation the rights to use, copy, modify, merge, publish,
40
+ distribute, sublicense, and/or sell copies of the Software, and to
41
+ permit persons to whom the Software is furnished to do so, subject to
42
+ the following conditions:
43
+
44
+ The above copyright notice and this permission notice shall be
45
+ included in all copies or substantial portions of the Software.
46
+
47
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
48
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
50
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
51
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
52
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
53
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/story-helper.rb'
6
+ require './lib/story-helper/version'
7
+
8
+ PKG_NAME = "story-helper"
9
+ PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
10
+ version = StoryHelper::Version::STRING.dup
11
+ if ENV['SNAPSHOT'].to_i == 1
12
+ version << "." << Time.now.utc.strftime("%Y%m%d%H%M%S")
13
+ end
14
+ PKG_VERSION = version
15
+ PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
16
+
17
+ Hoe.new(PKG_NAME, PKG_VERSION) do |p|
18
+ p.rubyforge_name = 'johntrupiano' # if different than lowercase project name
19
+ p.developer('John Trupiano', 'jtrupiano@gmail.com')
20
+ p.name = PKG_NAME
21
+ p.version = PKG_VERSION
22
+ #p.platform = Gem::Platform::RUBY
23
+ p.description = %q(A set of helpers/features to aid in implementing the StoryHelper concept in rails apps)
24
+ p.summary = p.description # More details later??
25
+ p.remote_rdoc_dir = PKG_NAME # Release to /PKG_NAME
26
+ # p.changes = p.paragraphs_of('CHANGELOG', 0..1).join("\n\n")
27
+ p.need_zip = true
28
+ p.need_tar = false
29
+ end
30
+
31
+ # vim: syntax=Ruby
@@ -0,0 +1,87 @@
1
+ #!/usr/bin/env ruby
2
+ # storify
3
+ # Author: John Trupiano
4
+ #
5
+ # Steps
6
+ # 1) Check if lib/story_helper.rb exists-- if not, let's place it in there
7
+ # 2) Check if lib/story_accessors.rb exists-- if not, let's place it in there
8
+ # 3) Check if lib/tasks/story_helper.rake exists-- if not, let's place it in there
9
+ # 4) Tell them to update their test_helper.rb file
10
+ #
11
+ # 5) How else can we help the user out?
12
+ # Can we auto-convert their fixtures?
13
+ # How about creating default StoryAccessors sections for each model?
14
+ # What else??
15
+
16
+ require 'fileutils'
17
+ include FileUtils
18
+
19
+ #require 'yaml'
20
+
21
+ class Storify
22
+
23
+ def self.run!
24
+ rails_root = ARGV[0] || '.'
25
+ puts "rails_root: #{rails_root}"
26
+
27
+ # lib/story_helper.rb
28
+ story_helper_rb = File.join(rails_root, 'lib', 'story_helper.rb')
29
+ gem_story_helper_rb = File.dirname(__FILE__) + '/../lib/story-helper/conf/story_helper.rb'
30
+ copy_if_missing(gem_story_helper_rb, story_helper_rb)
31
+
32
+ # lib/story_accessors.rb
33
+ story_accessors_rb = File.join(rails_root, 'lib', 'story_accessors.rb')
34
+ gem_story_accessors_rb = File.dirname(__FILE__) + '/../lib/story-helper/conf/story_accessors.rb'
35
+ copy_if_missing(gem_story_accessors_rb, story_accessors_rb)
36
+
37
+ # lib/tasks/story_helper.rake
38
+ story_helper_rake = File.join(rails_root, 'lib', 'tasks', 'story_helper.rake')
39
+ gem_story_helper_rake = File.dirname(__FILE__) + '/../lib/story-helper/conf/story_helper.rake'
40
+ copy_if_missing(gem_story_helper_rake, story_helper_rake)
41
+ end
42
+
43
+ def self.cleanup!
44
+ rails_root = ARGV[0] || '.'
45
+ puts "rails_root: #{rails_root}"
46
+
47
+ # lib/story_helper.rb
48
+ story_helper_rb = File.join(rails_root, 'lib', 'story_helper.rb')
49
+ delete_unless_missing(story_helper_rb)
50
+
51
+ # lib/story_accessors.rb
52
+ story_accessors_rb = File.join(rails_root, 'lib', 'story_accessors.rb')
53
+ delete_unless_missing(story_accessors_rb)
54
+
55
+ # lib/tasks/story_helper.rake
56
+ story_helper_rake = File.join(rails_root, 'lib', 'tasks', 'story_helper.rake')
57
+ delete_unless_missing(story_helper_rake)
58
+ end
59
+
60
+ private
61
+ def self.copy_if_missing(from, to)
62
+ if !File.exists?(to)
63
+ puts "cp #{from} #{to}"
64
+ cp(from, to)
65
+ else
66
+ puts "#{to} already exists"
67
+ end
68
+ end
69
+
70
+ def self.delete_unless_missing(file)
71
+ if File.exists?(file)
72
+ cmd = "rm #{file}"
73
+ puts cmd
74
+ rm_f(file)
75
+ else
76
+ puts "Deleting #{file}....doesn't exist."
77
+ end
78
+ end
79
+
80
+ end
81
+
82
+ if ARGV.size > 1 && ARGV[1] == "cleanup"
83
+ Storify.cleanup!
84
+ else
85
+ Storify.run!
86
+ puts "Don't forget to include StoryAccessors::Methods into your test_helper."
87
+ end
File without changes
@@ -0,0 +1,38 @@
1
+ # Defines a basic cached accessor
2
+ def meta_accessor(accessor, klass, find_by_field="name", field_value=accessor)
3
+ src = <<-RUBY
4
+ def #{accessor}(reload=false)
5
+ return @#{accessor} if @#{accessor} && !reload
6
+ @#{accessor} = #{klass.to_s}.find_by_#{find_by_field}("#{field_value}")
7
+ end
8
+ RUBY
9
+ class_eval src, __FILE__, __LINE__
10
+ end
11
+
12
+ # Generalized accessor functor
13
+ def scoped_meta_accessor(model_str, key, value, scoped_type, scoped_type_id, default_accessor_str)
14
+ scoped_field = "#{scoped_type}_id"
15
+ src = <<-RUBY
16
+ def #{key}(#{scoped_type}=#{default_accessor_str}, reload=false)
17
+ key = "@#{key}_" + #{scoped_type}.#{scoped_type_id}.to_s.gsub(' ', '_')
18
+ if reload || !instance_variable_defined?(key.to_sym)
19
+ instance_variable_set(key.to_sym, #{model_str}.find_by_name_and_#{scoped_field}(%q(#{value}), #{scoped_type}.id))
20
+ end
21
+ instance_variable_get(key.to_sym)
22
+ end
23
+ RUBY
24
+ class_eval src, __FILE__, __LINE__
25
+ end
26
+
27
+ module StoryAccessors
28
+
29
+ module Methods
30
+
31
+ ####### Accounts #######
32
+ # %w(amy sarah joanne julia erin).each do |acct|
33
+ # meta_accessor(acct.to_s, Account, "login")
34
+ # end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,44 @@
1
+ #############################
2
+ # Rewrites the test tasks to enforce StoryHelper usage
3
+ #############################
4
+
5
+ namespace :db do
6
+ desc "Load dev data into the current environment's database."
7
+ task :load_stories => :environment do
8
+ StoryHelper.purge_all_data
9
+ # Rake::Task['db:fixtures:load'].invoke
10
+ StoryHelper.load_all
11
+ end
12
+ end
13
+
14
+ # First, delete the Tasks we wish to override
15
+ ["functionals", "units", "integrations", "all"].each do |tt|
16
+ Rake.application.send(:eval, "@tasks.delete('test:#{tt}')")
17
+ end
18
+
19
+ # Now, override them!
20
+ namespace :test do
21
+ Rake::TestTask.new(:units) do |t|
22
+ t.libs << "test"
23
+ t.pattern = 'test/unit/**/*_test.rb'
24
+ t.verbose = true
25
+ end
26
+ Rake::Task['test:units'].comment = "*StoryHelper: Run the unit tests in test/unit. Your test data MUST be preloaded into the database."
27
+
28
+ Rake::TestTask.new(:functionals) do |t|
29
+ t.libs << "test"
30
+ t.pattern = 'test/functional/**/*_test.rb'
31
+ t.verbose = true
32
+ end
33
+ Rake::Task['test:functionals'].comment = "StoryHelper: Run the functional tests in test/functional. Your test data MUST be preloaded into the database."
34
+
35
+ Rake::TestTask.new(:integrations) do |t|
36
+ t.libs << "test"
37
+ t.pattern = 'test/integration/**/*_test.rb'
38
+ t.verbose = true
39
+ end
40
+ Rake::Task['test:integrations'].comment = "StoryHelper: Run the integration tests in test/integration. Your test data MUST be preloaded into the database."
41
+
42
+ desc "StoryHelper: Run unit and functional tests. Your test data MUST be preloaded into the database."
43
+ task :all => [:units, :functionals, :integrations]
44
+ end
@@ -0,0 +1,17 @@
1
+ # Exposes methods that create dev and test data. Loads things from a "user story"
2
+ # perspective.
3
+ class StoryHelper
4
+ self.extend StoryAccessors::Methods
5
+
6
+ def self.load_all
7
+ # Load data via Ruby -- your models are accessible here, so use those functions to create complex data!
8
+ #self.load_all
9
+ end
10
+
11
+ #
12
+ def self.purge_all_data
13
+ # Manually delete from all models and non-model join tables
14
+ #self.purge_all_data
15
+ end
16
+
17
+ end
@@ -0,0 +1,44 @@
1
+ class ActionController::TestCase
2
+ extend StoryAccessors::Methods # this probably isn't the right thing to do....
3
+
4
+ # allows us to use the self.log_in function to log in a certain user
5
+ @@scoped_session = {}
6
+
7
+ # Used as a directive at the top of a functional test (to blanket log in this user)
8
+ # def self.log_in(user)
9
+ # #puts "logging in #{user.email}"
10
+ # @@scoped_session.merge!({:user_id => user.id})
11
+ # end
12
+
13
+ # invoke in setup (or within a specific test to override the setup method)
14
+ def log_in(user)
15
+ @@scoped_session[:user_id] = user.id
16
+ end
17
+
18
+ def log_out
19
+ @@scoped_session.delete(:user_id)
20
+ end
21
+
22
+ # Used within a test to log in a specific user for a single test
23
+ # def log_in(user)
24
+ # old_user_id = @@scoped_session[:user_id]
25
+ # @@scoped_session[:user_id] = user.id
26
+ # yield
27
+ # ensure
28
+ # @@scoped_session[:user_id] = old_user_id
29
+ # end
30
+
31
+ [:get, :post, :put, :delete].each do |meth|
32
+ src = <<-END_OF_SRC
33
+ def #{meth.to_s}(action, parameters = nil, session = {}, flash = nil)
34
+ super(action, parameters, session.merge(@@scoped_session || {}), flash)
35
+ end
36
+ END_OF_SRC
37
+ class_eval src, __FILE__, __LINE__
38
+ end
39
+
40
+ def xhr(request_method, action, parameters = nil, session = {}, flash = nil)
41
+ super(request_method, action, parameters, session.merge(@@scoped_session || {}), flash)
42
+ end
43
+
44
+ end
@@ -0,0 +1,20 @@
1
+ module StoryHelper
2
+ module Version #:nodoc:
3
+ # A method for comparing versions of required modules. It expects two
4
+ # arrays of integers as parameters, the first being the minimum version
5
+ # required, and the second being the actual version available. It returns
6
+ # true if the actual version is at least equal to the required version.
7
+ def self.check(required, actual) #:nodoc:
8
+ required = required.map { |v| "%06d" % v }.join(".")
9
+ actual = actual.map { |v| "%06d" % v }.join(".")
10
+ return actual >= required
11
+ end
12
+
13
+ MAJOR = 0
14
+ MINOR = 1
15
+ TINY = 0
16
+
17
+ STRING = [MAJOR, MINOR, TINY].join(".")
18
+ end
19
+ end
20
+
@@ -0,0 +1,35 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{story-helper}
3
+ s.version = "0.1.0"
4
+
5
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
+ s.authors = ["John Trupiano"]
7
+ s.date = %q{2008-09-16}
8
+ s.default_executable = %q{storify}
9
+ s.description = %q{A set of helpers/features to aid in implementing the StoryHelper concept in rails apps}
10
+ s.email = ["jtrupiano@gmail.com"]
11
+ s.executables = ["storify"]
12
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
13
+ s.files = ["History.txt", "Manifest.txt", "README.txt", "Rakefile", "bin/storify", "lib/story-helper.rb", "lib/story-helper/conf/story_accessors.rb", "lib/story-helper/conf/story_helper.rake", "lib/story-helper/conf/story_helper.rb", "lib/story-helper/version.rb", "story-helper.gemspec", "test/rails_version_test.rb", "test/test_story_helper.rb"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/jtrupiano/story-helper}
16
+ s.rdoc_options = ["--main", "README.txt"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{johntrupiano}
19
+ s.rubygems_version = %q{1.2.0}
20
+ s.summary = %q{A set of helpers/features to aid in implementing the StoryHelper concept in rails apps}
21
+ s.test_files = ["test/test_story_helper.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if current_version >= 3 then
28
+ s.add_development_dependency(%q<hoe>, [">= 1.7.0"])
29
+ else
30
+ s.add_dependency(%q<hoe>, [">= 1.7.0"])
31
+ end
32
+ else
33
+ s.add_dependency(%q<hoe>, [">= 1.7.0"])
34
+ end
35
+ end
@@ -0,0 +1,38 @@
1
+ require 'test/unit'
2
+ require 'fileutils'
3
+ require 'yaml'
4
+
5
+ class RailsVersionTest < Test::Unit::TestCase
6
+ include FileUtils
7
+
8
+ def test_rails_two_one_one
9
+ # Clean up an old run if necessary
10
+ rm_rf('rails211test')
11
+
12
+ cp_r('rails211', 'rails211test')
13
+ system("storify rails211test")
14
+
15
+ # config_root = File.join('rails211test', 'config')
16
+ #
17
+ # assert File.exists?(File.join(config_root, 'postboot.rb'))
18
+ #
19
+ # env_rb = File.open(File.join(config_root, 'environment.rb')).read
20
+ # assert env_rb.include?("require File.join(File.dirname(__FILE__), 'postboot')")
21
+ #
22
+ # %w(development test demo staging production).each do |env|
23
+ # assert File.directory?(File.join(config_root, env))
24
+ # end
25
+ #
26
+ # %w(development test production).each do |env|
27
+ # %w(database.yml environment.rb).each do |f|
28
+ # assert File.exists?(File.join(config_root, env, f))
29
+ # end
30
+ # puts "Testing #{env}"
31
+ # y = YAML.load_file(File.join(config_root, env, 'database.yml'))
32
+ # assert_equal 1, y.keys.size
33
+ # assert_equal env, y.keys.first
34
+ # end
35
+ # ensure
36
+ # rm_rf('rails210test')
37
+ end
38
+ end
File without changes
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: story-helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Trupiano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-21 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.7.0
24
+ version:
25
+ description: A set of helpers/features to aid in implementing the StoryHelper concept in rails apps
26
+ email:
27
+ - jtrupiano@gmail.com
28
+ executables:
29
+ - storify
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ files:
37
+ - History.txt
38
+ - Manifest.txt
39
+ - README.txt
40
+ - Rakefile
41
+ - bin/storify
42
+ - lib/story-helper.rb
43
+ - lib/story-helper/conf/story_accessors.rb
44
+ - lib/story-helper/conf/story_helper.rake
45
+ - lib/story-helper/conf/story_helper.rb
46
+ - lib/story-helper/conf/test_helper_extensions.rb
47
+ - lib/story-helper/version.rb
48
+ - story-helper.gemspec
49
+ - test/rails_version_test.rb
50
+ - test/test_story_helper.rb
51
+ has_rdoc: true
52
+ homepage: http://github.com/jtrupiano/story-helper
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --main
56
+ - README.txt
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project: johntrupiano
74
+ rubygems_version: 1.2.0
75
+ signing_key:
76
+ specification_version: 2
77
+ summary: A set of helpers/features to aid in implementing the StoryHelper concept in rails apps
78
+ test_files:
79
+ - test/test_story_helper.rb