factory_scenarios 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,27 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "rails", "~> 3.0"
7
+ gem "dkastner-moneta", "~> 0.7"
8
+ gem "factory_girl", "~> 2", :require => false
9
+ gem "warden", "~> 1.0"
10
+ gem "haml", "~> 3.0"
11
+ gem "hashie", "~> 1.1"
12
+
13
+ # Add dependencies to develop your gem here.
14
+ # Include everything needed to run rake, tests, features, etc.
15
+ group :development do
16
+ gem "shoulda", ">= 0"
17
+ gem "bundler", "~> 1.1.rc8"
18
+ gem "jeweler", "~> 1.8.3"
19
+
20
+ platform :ruby_18 do
21
+ gem "rcov", ">= 0"
22
+ end
23
+
24
+ platform :ruby_19 do
25
+ gem "simplecov", ">= 0"
26
+ end
27
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,118 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionmailer (3.2.2)
5
+ actionpack (= 3.2.2)
6
+ mail (~> 2.4.0)
7
+ actionpack (3.2.2)
8
+ activemodel (= 3.2.2)
9
+ activesupport (= 3.2.2)
10
+ builder (~> 3.0.0)
11
+ erubis (~> 2.7.0)
12
+ journey (~> 1.0.1)
13
+ rack (~> 1.4.0)
14
+ rack-cache (~> 1.1)
15
+ rack-test (~> 0.6.1)
16
+ sprockets (~> 2.1.2)
17
+ activemodel (3.2.2)
18
+ activesupport (= 3.2.2)
19
+ builder (~> 3.0.0)
20
+ activerecord (3.2.2)
21
+ activemodel (= 3.2.2)
22
+ activesupport (= 3.2.2)
23
+ arel (~> 3.0.2)
24
+ tzinfo (~> 0.3.29)
25
+ activeresource (3.2.2)
26
+ activemodel (= 3.2.2)
27
+ activesupport (= 3.2.2)
28
+ activesupport (3.2.2)
29
+ i18n (~> 0.6)
30
+ multi_json (~> 1.0)
31
+ arel (3.0.2)
32
+ builder (3.0.0)
33
+ dkastner-moneta (0.7.0)
34
+ erubis (2.7.0)
35
+ factory_girl (2.4.2)
36
+ activesupport
37
+ git (1.2.5)
38
+ haml (3.1.4)
39
+ hashie (1.2.0)
40
+ hike (1.2.1)
41
+ i18n (0.6.0)
42
+ jeweler (1.8.3)
43
+ bundler (~> 1.0)
44
+ git (>= 1.2.5)
45
+ rake
46
+ rdoc
47
+ journey (1.0.3)
48
+ json (1.6.5)
49
+ mail (2.4.4)
50
+ i18n (>= 0.4.0)
51
+ mime-types (~> 1.16)
52
+ treetop (~> 1.4.8)
53
+ mime-types (1.17.2)
54
+ multi_json (1.1.0)
55
+ polyglot (0.3.3)
56
+ rack (1.4.1)
57
+ rack-cache (1.2)
58
+ rack (>= 0.4)
59
+ rack-ssl (1.3.2)
60
+ rack
61
+ rack-test (0.6.1)
62
+ rack (>= 1.0)
63
+ rails (3.2.2)
64
+ actionmailer (= 3.2.2)
65
+ actionpack (= 3.2.2)
66
+ activerecord (= 3.2.2)
67
+ activeresource (= 3.2.2)
68
+ activesupport (= 3.2.2)
69
+ bundler (~> 1.0)
70
+ railties (= 3.2.2)
71
+ railties (3.2.2)
72
+ actionpack (= 3.2.2)
73
+ activesupport (= 3.2.2)
74
+ rack-ssl (~> 1.3.2)
75
+ rake (>= 0.8.7)
76
+ rdoc (~> 3.4)
77
+ thor (~> 0.14.6)
78
+ rake (0.9.2.2)
79
+ rcov (1.0.0)
80
+ rdoc (3.12)
81
+ json (~> 1.4)
82
+ shoulda (3.0.1)
83
+ shoulda-context (~> 1.0.0)
84
+ shoulda-matchers (~> 1.0.0)
85
+ shoulda-context (1.0.0)
86
+ shoulda-matchers (1.0.0)
87
+ simplecov (0.6.1)
88
+ multi_json (~> 1.0)
89
+ simplecov-html (~> 0.5.3)
90
+ simplecov-html (0.5.3)
91
+ sprockets (2.1.2)
92
+ hike (~> 1.2)
93
+ rack (~> 1.0)
94
+ tilt (~> 1.1, != 1.3.0)
95
+ thor (0.14.6)
96
+ tilt (1.3.3)
97
+ treetop (1.4.10)
98
+ polyglot
99
+ polyglot (>= 0.3.1)
100
+ tzinfo (0.3.32)
101
+ warden (1.1.1)
102
+ rack (>= 1.0)
103
+
104
+ PLATFORMS
105
+ ruby
106
+
107
+ DEPENDENCIES
108
+ bundler (~> 1.1.rc8)
109
+ dkastner-moneta (~> 0.7)
110
+ factory_girl (~> 2)
111
+ haml (~> 3.0)
112
+ hashie (~> 1.1)
113
+ jeweler (~> 1.8.3)
114
+ rails (~> 3.0)
115
+ rcov
116
+ shoulda
117
+ simplecov
118
+ warden (~> 1.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Collin Miller
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "factory_scenarios"
16
+ gem.homepage = "http://github.com/collin/factory_scenarios"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Use your factories to help you navigate your application state.}
19
+ gem.description = %Q{Build your factory and log-in in a single click. Also see what your emails look like and enter your workflows by clickig on the links in the email}
20
+ gem.email = "collintmiller@gmail.com"
21
+ gem.authors = ["Collin Miller"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ #require 'rcov/rcovtask'
37
+ #Rcov::RcovTask.new do |test|
38
+ # test.libs << 'test'
39
+ # test.pattern = 'test/**/test_*.rb'
40
+ # test.verbose = true
41
+ #end
42
+
43
+ task :default => :test
44
+
45
+ #require 'rake/rdoctask'
46
+ #Rake::RDocTask.new do |rdoc|
47
+ # version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+ #
49
+ # rdoc.rdoc_dir = 'rdoc'
50
+ # rdoc.title = "factory_scenarios #{version}"
51
+ # rdoc.rdoc_files.include('README*')
52
+ # rdoc.rdoc_files.include('lib/**/*.rb')
53
+ #end
data/Readme.mdown ADDED
@@ -0,0 +1,127 @@
1
+ # factory_scenarios/mail_preview
2
+
3
+ Login as users defined in your FactoryGirl factories.
4
+
5
+ Preview email while logged in as users defined in your FactoryGirl factories.
6
+
7
+ ## Installation
8
+
9
+ * Add the gem to your gemfile
10
+ group :development do
11
+ gem 'factory_scenarios'
12
+ end
13
+ * Run bundle install
14
+
15
+ ## Configuration
16
+
17
+ * FactoryScenarios is a mountable engine. Add a line like this to your routes.rb file:
18
+
19
+ ```ruby
20
+ if Rails.env.to_s != 'production'
21
+ mount FactoryScenarios::Engine, :at => '/factory_scenarios'
22
+ end
23
+ ```
24
+
25
+ * Currently there are two configuration settings
26
+ - iframe_width: sets the width of the iframe that your site will appear in, defaults to 1000px
27
+ - user_class: your users class, defaults to User
28
+ * <b> Important Note </b>
29
+ - You need to have active_reload installed in your application to utilize this gem successfully. The consequences of not
30
+ having this gem install will be errors when trying to login using your factories.
31
+
32
+ ## Example Configuration
33
+
34
+ Create an initializer file called app/config/initializers/factory_scenarios.rb, add the following:
35
+
36
+ ```ruby
37
+ if Rails.env.to_s != 'production'
38
+ FactoryScenarios.config.user_class = 'Spree::User'
39
+ FactoryScenarios.config.iframe_width = '1400px'
40
+ end
41
+ ```
42
+
43
+ To redirect to a specific location in your application after logging in as a scenario
44
+ define handle_factory_scenario in your ApplicationController.
45
+
46
+ ```ruby
47
+ # Be sure to use main_app for url_generation as this method will be called
48
+ # from inside the engine controllers
49
+ def handle_factory_scenario
50
+ redirect_to main_app.root_url(subdomain: current_user.subdomain)
51
+ end
52
+ ```
53
+
54
+ #### ( Seems like this should be a configuration option rather than a method defined on the app controller no?)
55
+
56
+ Specify your mail previews in a file "config/mail_preview.rb"
57
+
58
+ ```ruby
59
+ FactoryScenarios::Mail.registrations do
60
+ register "Org Membership Invitation (no login)" do
61
+ mailer MembershipMailer
62
+ message :approval_message
63
+ message_args { Factory.create(:membership) }
64
+ end
65
+
66
+ register "Org Membership Invitation (correct login)" do
67
+ mailer MembershipMailer
68
+ message :approval_message
69
+ message_args { Factory.create(:membership) }
70
+
71
+ # Arguments to login_as block are the return value of the message_args block.
72
+ login_as { |membership| membership.user }
73
+ end
74
+
75
+ register "Org Membership Invitation (wrong login)" do
76
+ mailer MembershipMailer
77
+ message :approval_message
78
+ message_args { Factory.create(:membership) }
79
+ login_as { Factory.create :user }
80
+ end
81
+
82
+ register "User registration" do
83
+ mailer Devise::Mailer
84
+ message :confirmation_instructions
85
+ message_args { Factory.create(:unconfirmed_user) }
86
+ end
87
+
88
+ register "Reset password instructions" do
89
+ mailer Devise::Mailer
90
+ message :reset_password_instructions
91
+ message_args { Factory.create(:recoverable_user) }
92
+ end
93
+ end
94
+
95
+ ```
96
+
97
+ ## Factory Scenarios Screenshot
98
+
99
+ ![Facotory Scenarios](http://img.skitch.com/20120316-qjyjksx9cbx9796c1j8rb29565.png)
100
+
101
+ ## Mail Preview Screenshot
102
+
103
+ ![Mail Preview](https://img.skitch.com/20120316-pasihu1qp4t3mkuia9kt5km4ju.png)
104
+
105
+
106
+ ## Todo
107
+ * Remove hard coded reference to BaseHelper, add as configuration option.
108
+
109
+ ## Contributing to factory_scenarios
110
+
111
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
112
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
113
+ * Fork the project
114
+ * Start a feature/bugfix branch
115
+ * Commit and push until you are happy with your contribution
116
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
117
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
118
+
119
+ ## Contributors
120
+
121
+ Original gem written by Collin Miller. Updated for Rails 3.1 by David Guthu.
122
+
123
+ ## Copyright
124
+
125
+ Copyright (c) 2011 Collin Miller. See LICENSE.txt for
126
+ further details.
127
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,7 @@
1
+ jQuery ($) ->
2
+ $("li a").on "click", (event) ->
3
+ $(".current").removeClass("current")
4
+ $(this).closest("li").addClass("current")
5
+
6
+
7
+ $("iframe").attr "src", $(".current a:first-child").attr("href")
@@ -0,0 +1,72 @@
1
+ body
2
+ font-family: sans-serif
3
+ margin: 0
4
+ padding: 0
5
+ hr
6
+ padding: 0
7
+ margin: 0
8
+ border-bottom: 0.5px solid #999
9
+ h1
10
+ font-size: 16px
11
+ body > ul
12
+ width: 20%
13
+ min-width: 150px
14
+ ul
15
+ margin: 0
16
+ padding: 0
17
+ li
18
+ list-style-type: none
19
+ border-bottom: 1px dotted #999
20
+ margin: 0
21
+ padding: 1em 0
22
+ display: block
23
+ min-width: 170px
24
+ overflow: hidden
25
+ padding-left: 1em
26
+
27
+ &.current
28
+ background-color: #ffc
29
+
30
+ &:last-child
31
+ border-bottom: none
32
+
33
+ form
34
+ display: inline-block
35
+ margin: 0
36
+
37
+ .actions
38
+ float: right
39
+ margin-right: 20px
40
+
41
+
42
+ a
43
+ font-size: 12px
44
+ &.reload
45
+ color: red
46
+ &:hover
47
+ background: red
48
+ color: white
49
+ iframe
50
+ border: none
51
+ border-left: 3px solid #ccf
52
+
53
+ #mail-preview-container
54
+ h1
55
+ font-size: 16px
56
+ h2
57
+ font-size: 12px
58
+ > ul > ul
59
+ padding-left: 40px
60
+
61
+ .headers
62
+ background-color: #dadada
63
+ padding-top: 5px
64
+ p
65
+ height: 24px
66
+ margin: 0
67
+ span
68
+ display: inline-block
69
+ width: 100px
70
+ margin-right: 10px
71
+ font-family: monospace
72
+ text-align: right
@@ -0,0 +1,20 @@
1
+ module FactoryScenarios
2
+ class EngineController < ApplicationController
3
+ private
4
+
5
+ def login_to_scenario(user_for_scenario=nil)
6
+ reset_session
7
+
8
+ return unless current_user
9
+ sign_out(current_user)
10
+
11
+ return unless user_for_scenario
12
+ @current_user = user_for_scenario
13
+ sign_in(:user, user_for_scenario)
14
+ end
15
+
16
+ def set_iframe_params
17
+ @width = FactoryScenarios.config.iframe_width
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ module FactoryScenarios
2
+ class MailPreviewsController < EngineController
3
+ def index
4
+ set_iframe_params
5
+ @previews = FactoryScenarios::Mail.previews
6
+ render layout: false
7
+ end
8
+
9
+ def show
10
+ preview = FactoryScenarios::Mail.previews[params[:mailer]][params[:name]]
11
+ login_to_scenario preview.login_as
12
+ @message = preview.mailer_message
13
+ render layout: nil
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ module FactoryScenarios
2
+ class ScenariosController < EngineController
3
+
4
+ def index
5
+ set_iframe_params
6
+ @scenarios = Scenario.all
7
+ render :layout => nil
8
+ end
9
+
10
+ def enact
11
+ user_for_scenario = Scenario.find(params[:id]).enact(!!params[:clear])
12
+
13
+ login_to_scenario user_for_scenario
14
+
15
+ if respond_to?(:handle_factory_scenario)
16
+ handle_factory_scenario
17
+ else
18
+ redirect_to "/"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,73 @@
1
+ module FactoryScenarios
2
+ class Scenario
3
+
4
+ def storage
5
+ FactoryScenarios.storage
6
+ end
7
+
8
+ def self.all
9
+ FactoryGirl.factories.find_all{|fact| fact.build_class.to_s == FactoryScenarios.config.user_class }.map do |factory|
10
+ new factory
11
+ end
12
+ end
13
+
14
+ def self.find(name)
15
+ all.find{|scenario| scenario.name == name }
16
+ end
17
+
18
+ def self.clear_all
19
+ all.each(&:clear)
20
+ end
21
+
22
+ def initialize(factory)
23
+ @factory = factory
24
+ end
25
+
26
+ def persisted?
27
+ !!find_user
28
+ end
29
+
30
+ def name
31
+ @factory.name.to_s
32
+ end
33
+
34
+ def enact(do_clear=false)
35
+ clear if do_clear
36
+ if persisted?
37
+ user = find_user
38
+ else
39
+ user = Factory.create(self.name)
40
+ self.user_id = user.id
41
+ end
42
+
43
+ return user
44
+ end
45
+
46
+ def user_class
47
+ FactoryScenarios.config.user_class.constantize
48
+ end
49
+
50
+ def find_user
51
+ user_class.where(id: user_id).first
52
+ end
53
+
54
+ def user_id
55
+ user_id = storage[self.name].to_i
56
+ end
57
+
58
+ def user_id=(id)
59
+ storage[self.name] = id
60
+ end
61
+
62
+ # This doesn't delete associated records.
63
+ # It just lets you re-set the named factory.
64
+ def clear
65
+ storage.delete(self.name)
66
+ end
67
+
68
+ def to_param
69
+ name
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,30 @@
1
+ =javascript_include_tag "jquery"
2
+ =javascript_include_tag "factory_scenarios"
3
+ =stylesheet_link_tag "factory_scenarios"
4
+
5
+ %h1 Mail Preview
6
+ %ul#mail-preview-container
7
+ - if @previews.any?
8
+ %ul
9
+ - @previews.each do |mailer, previews|
10
+ %li
11
+ %h2= mailer.titlecase
12
+ %ul
13
+ - previews.each do |name, preview|
14
+ %li= link_to name, show_mail_preview_path(mailer, name: name), target: "dynamic"
15
+
16
+ - else
17
+ %h2 There are no defined mail previews.
18
+ %p Make sure you are defining yoru previews.
19
+ %pre
20
+ %code
21
+ :plain
22
+ FactoryScenarios::Mail.register "Org Membership Invitation" do
23
+ membership = Factory.create(:membership)
24
+ mailer MembershipMailer
25
+ message :approval_message
26
+ message_args membership
27
+ end
28
+
29
+
30
+ %iframe(name="dynamic" id="dynamic" style="position: fixed; top: 0; right:0; width: #{@width}; height: 100%; background: white;")
@@ -0,0 +1,19 @@
1
+ =stylesheet_link_tag "factory_scenarios"
2
+ .headers
3
+ %p
4
+ %span Subject:
5
+ = @message.subject
6
+ %p
7
+ %span From:
8
+ = @message.from.join(", ")
9
+ %p
10
+ %span To:
11
+ = @message.to.join(", ")
12
+ %p
13
+ %span Sender:
14
+ = @message.sender
15
+
16
+ %hr
17
+
18
+ %irame(style="border: none; margin: 0; padding: 0; background: white;")
19
+ = @message.body.decoded.html_safe
@@ -0,0 +1,19 @@
1
+ =javascript_include_tag "jquery"
2
+ =javascript_include_tag "factory_scenarios"
3
+ =stylesheet_link_tag "factory_scenarios"
4
+
5
+ %h1 Factory Scenarios
6
+ %ul#factory-scenarios-container
7
+ - if @scenarios.any?
8
+ - for scenario in @scenarios
9
+ %li(class="#{"current" if scenario.find_user && scenario.find_user == current_user}")
10
+ "#{scenario.name.titlecase}"
11
+ .actions
12
+ = link_to "Login", enact_scenario_path(scenario), :target => :dynamic, :id => "enact_#{scenario.name}"
13
+ = link_to "reset data & login", enact_scenario_path(scenario, :clear => true), :class => :reload, :target => :dynamic, :id => "reload_#{scenario.name}"
14
+ - else
15
+ %h2 There are no defined scenarios.
16
+ %p Make sure you are loading your scenarios.
17
+ %p Only scenarios for your User class will be loaded as scenarios.
18
+
19
+ %iframe(name="dynamic" id="dynamic" style=" position: fixed; top: 0; right:0; width: #{@width}; height: 100%; background: white;")
data/config/routes.rb ADDED
@@ -0,0 +1,12 @@
1
+ FactoryScenarios::Engine.routes.draw do
2
+ root :to => 'scenarios#index'
3
+
4
+ resources :scenarios do
5
+ member do
6
+ get :enact
7
+ end
8
+ end
9
+
10
+ resources :mail_previews
11
+ match "/mail_previews/:mailer/:name" => "mail_previews#show", as: :show_mail_preview
12
+ end
@@ -0,0 +1,92 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "factory_scenarios"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Collin Miller"]
12
+ s.date = "2012-03-16"
13
+ s.description = "Build your factory and log-in in a single click. Also see what your emails look like and enter your workflows by clickig on the links in the email"
14
+ s.email = "collintmiller@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE.txt",
23
+ "Rakefile",
24
+ "Readme.mdown",
25
+ "VERSION",
26
+ "app/assets/javascripts/factory_scenarios.coffee",
27
+ "app/assets/stylesheets/factory_scenarios.css.sass",
28
+ "app/controllers/factory_scenarios/engine_controller.rb",
29
+ "app/controllers/factory_scenarios/mail_previews_controller.rb",
30
+ "app/controllers/factory_scenarios/scenarios_controller.rb",
31
+ "app/models/factory_scenarios/scenario.rb",
32
+ "app/views/factory_scenarios/mail_previews/index.html.haml",
33
+ "app/views/factory_scenarios/mail_previews/show.html.haml",
34
+ "app/views/factory_scenarios/scenarios/index.html.haml",
35
+ "config/routes.rb",
36
+ "factory_scenarios.gemspec",
37
+ "lib/factory_scenarios.rb",
38
+ "lib/factory_scenarios/engine.rb",
39
+ "lib/factory_scenarios/mail.rb",
40
+ "lib/factory_scenarios/mail/preview.rb",
41
+ "test/helper.rb",
42
+ "test/test_factory_scenarios.rb"
43
+ ]
44
+ s.homepage = "http://github.com/collin/factory_scenarios"
45
+ s.licenses = ["MIT"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = "1.8.10"
48
+ s.summary = "Use your factories to help you navigate your application state."
49
+
50
+ if s.respond_to? :specification_version then
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
+ s.add_runtime_dependency(%q<rails>, ["~> 3.0"])
55
+ s.add_runtime_dependency(%q<dkastner-moneta>, ["~> 0.7"])
56
+ s.add_runtime_dependency(%q<factory_girl>, ["~> 2"])
57
+ s.add_runtime_dependency(%q<warden>, ["~> 1.0"])
58
+ s.add_runtime_dependency(%q<haml>, ["~> 3.0"])
59
+ s.add_runtime_dependency(%q<hashie>, ["~> 1.1"])
60
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
61
+ s.add_development_dependency(%q<bundler>, ["~> 1.1.rc8"])
62
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
63
+ s.add_development_dependency(%q<rcov>, [">= 0"])
64
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
65
+ else
66
+ s.add_dependency(%q<rails>, ["~> 3.0"])
67
+ s.add_dependency(%q<dkastner-moneta>, ["~> 0.7"])
68
+ s.add_dependency(%q<factory_girl>, ["~> 2"])
69
+ s.add_dependency(%q<warden>, ["~> 1.0"])
70
+ s.add_dependency(%q<haml>, ["~> 3.0"])
71
+ s.add_dependency(%q<hashie>, ["~> 1.1"])
72
+ s.add_dependency(%q<shoulda>, [">= 0"])
73
+ s.add_dependency(%q<bundler>, ["~> 1.1.rc8"])
74
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
75
+ s.add_dependency(%q<rcov>, [">= 0"])
76
+ s.add_dependency(%q<simplecov>, [">= 0"])
77
+ end
78
+ else
79
+ s.add_dependency(%q<rails>, ["~> 3.0"])
80
+ s.add_dependency(%q<dkastner-moneta>, ["~> 0.7"])
81
+ s.add_dependency(%q<factory_girl>, ["~> 2"])
82
+ s.add_dependency(%q<warden>, ["~> 1.0"])
83
+ s.add_dependency(%q<haml>, ["~> 3.0"])
84
+ s.add_dependency(%q<hashie>, ["~> 1.1"])
85
+ s.add_dependency(%q<shoulda>, [">= 0"])
86
+ s.add_dependency(%q<bundler>, ["~> 1.1.rc8"])
87
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
88
+ s.add_dependency(%q<rcov>, [">= 0"])
89
+ s.add_dependency(%q<simplecov>, [">= 0"])
90
+ end
91
+ end
92
+
@@ -0,0 +1,74 @@
1
+ require "hashie/dash"
2
+ module FactoryScenarios
3
+ class AppConfiguration < Hashie::Dash
4
+ property :iframe_width
5
+ property :user_class
6
+ property :factory_scenarios_moneta_backend
7
+ property :factory_scenarios_moneta_config
8
+ end
9
+
10
+ module ::FactoryGirl
11
+ def self.register_factory(factory, options = {})
12
+ self.factories.class.class_eval {
13
+ def add_forced(item)
14
+ @items[item.name.to_sym] = item
15
+ end
16
+ }
17
+
18
+ self.factories.add_forced(factory)
19
+ end
20
+
21
+ def self.register_sequence(sequence, options = {})
22
+ self.sequences.class.class_eval {
23
+ def add_forced(item)
24
+ @items[item.name.to_sym] = item
25
+ end
26
+ }
27
+
28
+ self.sequences.add_forced(sequence)
29
+ end
30
+ end
31
+
32
+ def self.config
33
+ @config ||= AppConfiguration.new(
34
+ :user_class => 'User',
35
+ :iframe_width => '80%',
36
+ :factory_scenarios_moneta_backend => :YAML,
37
+ :factory_scenarios_moneta_config => {
38
+ :path => "#{Rails.root.to_s}/db/factory_scenarios.#{Rails.env}.yml"
39
+ }
40
+ )
41
+ end
42
+
43
+ class Engine < Rails::Engine
44
+ isolate_namespace FactoryScenarios
45
+ engine_name 'factory_scenarios'
46
+
47
+ initializer "paths" do
48
+ paths['factories'] = "#{Rails.root.to_s}/spec/factories"
49
+ paths['factories'] << "#{Rails.root.to_s}/factories"
50
+ paths['mail_previews'] = "#{Rails.root.to_s}/config/mail_preview.rb"
51
+ end
52
+
53
+ initializer "factory_scenarios.config", :before => :load_config_initializers do |app|
54
+ config
55
+ end
56
+
57
+ config.to_prepare do
58
+ root = Rails.application.config.root
59
+
60
+ for factories_path in FactoryScenarios::Engine.paths['factories']
61
+ globstring = (factories_path + "/**/*.rb").to_s
62
+
63
+ Dir[globstring].each do |factory|
64
+ load factory
65
+ end
66
+ end
67
+
68
+ preview_paths = FactoryScenarios::Engine.paths['mail_previews'].first
69
+ Dir[preview_paths].each do |previews|
70
+ load previews
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,49 @@
1
+ class FactoryScenarios::Mail::Preview
2
+ def mailer_message
3
+ mailer.send(message, *self.message_args)
4
+ end
5
+
6
+ def render
7
+ message
8
+ end
9
+
10
+ def login_as(&block)
11
+ if block_given?
12
+ @login_as = block
13
+ elsif @login_as
14
+ @login_as.call *self.message_args
15
+ end
16
+ end
17
+
18
+ def name(name=nil)
19
+ if name
20
+ @name = name
21
+ else
22
+ @name
23
+ end
24
+ end
25
+
26
+ def mailer(mailer=nil)
27
+ if mailer
28
+ @mailer = mailer
29
+ else
30
+ @mailer
31
+ end
32
+ end
33
+
34
+ def message(message=nil)
35
+ if message
36
+ @message = message
37
+ else
38
+ @message
39
+ end
40
+ end
41
+
42
+ def message_args(&block)
43
+ if block_given?
44
+ @message_args = block
45
+ else
46
+ @_args ||= @message_args.call
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,21 @@
1
+ module FactoryScenarios::Mail
2
+ autoload :Preview, "factory_scenarios/mail/preview"
3
+ @@previews = {}
4
+
5
+ def self.register(name, &block)
6
+ preview = Preview.new
7
+ preview.instance_eval(&block)
8
+ preview.name(name)
9
+ (@@previews[preview.mailer.name] ||= {})[name] = preview
10
+ preview
11
+ end
12
+
13
+ def self.registrations(&block)
14
+ @@previews = {}
15
+ instance_eval(&block)
16
+ end
17
+
18
+ def self.previews
19
+ @@previews
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ require "rails"
2
+ require "factory_girl"
3
+ require "haml"
4
+ require "warden"
5
+ require "moneta"
6
+
7
+ module FactoryScenarios
8
+ require "factory_scenarios/engine"
9
+ autoload :Mail, "factory_scenarios/mail"
10
+
11
+ def self.storage
12
+ require "moneta/adapters/#{FactoryScenarios.config.factory_scenarios_moneta_backend.to_s.downcase}"
13
+ storage_class = Moneta::Adapters.const_get(FactoryScenarios.config.factory_scenarios_moneta_backend)
14
+ storage_class.new FactoryScenarios.config.factory_scenarios_moneta_config
15
+ end
16
+
17
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'factory_scenarios'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,150 @@
1
+ require 'helper'
2
+ require 'pathname'
3
+ require 'fileutils'
4
+ require 'moneta/adapters/yaml'
5
+
6
+ # Mocks
7
+ def Object.meta_mock(method, _return)
8
+ (class << self; self end).send(:define_method, method) {|*args| _return }
9
+ end
10
+
11
+ require "./../factory_scenarios/app/models/factory_scenarios/scenario"
12
+ require "factory_girl"
13
+ require "active_record"
14
+ class ::User
15
+ def save!; true end
16
+ def id
17
+ @id ||= rand(10000).to_i
18
+ end
19
+ end
20
+ class ::NotUser; end
21
+ FactoryGirl.define do
22
+ factory :user do; end
23
+ factory :not_user do; end
24
+ factory :user_child, :parent => :user do; end
25
+ end
26
+
27
+ class TestFactoryScenarios < Test::Unit::TestCase
28
+ DEFAULT_FILE_PATH = "/db/factory_scenarios.development.yml"
29
+ context "storage" do
30
+ teardown do
31
+ FactoryScenarios.config.factory_scenarios_moneta_config[:path] = DEFAULT_FILE_PATH
32
+ end
33
+
34
+ should "has default configuration" do
35
+ assert_equal "User", FactoryScenarios.config.user_class
36
+ assert_equal "80%", FactoryScenarios.config.iframe_width
37
+ assert_equal :YAML, FactoryScenarios.config.factory_scenarios_moneta_backend
38
+ assert_equal DEFAULT_FILE_PATH, FactoryScenarios.config.factory_scenarios_moneta_config[:path]
39
+ end
40
+
41
+ should "create the default storage" do
42
+ temp = Tempfile.new("factory_scenarios").path + ".yml"
43
+ FactoryScenarios.config.factory_scenarios_moneta_config[:path] = temp
44
+ assert_equal Moneta::Adapters::YAML, FactoryScenarios.storage.class
45
+ end
46
+
47
+ end
48
+
49
+ context FactoryScenarios::Scenario do
50
+ setup do
51
+ @path = FactoryScenarios.config.factory_scenarios_moneta_config[:path] = "./test/factory_scenarios.test.yml"
52
+ @scenario = FactoryScenarios::Scenario.new(FactoryGirl.factories.first)
53
+ end
54
+
55
+ should "have a storage" do
56
+ assert_equal Moneta::Adapters::YAML, @scenario.storage.class
57
+ end
58
+
59
+ context "Scenario.all" do
60
+ should "return scenarios only for user factories" do
61
+ assert_equal ["user", "user_child"], FactoryScenarios::Scenario.all.map(&:name)
62
+ end
63
+ end
64
+
65
+ context "Scenario.find" do
66
+ should "find a scenario by factory name" do
67
+ assert_equal nil, FactoryScenarios::Scenario.find("not_user")
68
+ assert_equal "user", FactoryScenarios::Scenario.find("user").name
69
+ end
70
+ end
71
+
72
+ context "#enact" do
73
+ context "when persisted? is true" do
74
+ setup do
75
+ @user = User.new
76
+ User.meta_mock(:where, [@user])
77
+ end
78
+
79
+ should "clear if do_clear is true" do
80
+ @scenario.user_id = 20
81
+ @scenario.enact(true)
82
+ assert_equal 0, @scenario.user_id
83
+ end
84
+
85
+ should "not clear if do_clear isnt true" do
86
+ @scenario.user_id = 20
87
+ @scenario.enact
88
+ assert_equal 20, @scenario.user_id
89
+ end
90
+ end
91
+
92
+ should "create a new user if scenario isn't persisted and do_clear is true" do
93
+ User.meta_mock(:where, [])
94
+ assert_not_equal nil, @scenario.enact(true)
95
+ end
96
+ end
97
+
98
+ context "#find_user" do
99
+ should "return first found user" do
100
+ found = User.new
101
+ User.meta_mock(:where, [found])
102
+ assert_equal found, @scenario.find_user
103
+ end
104
+
105
+ should "return nothing when no users found" do
106
+ User.meta_mock(:where, [])
107
+ assert_equal nil, @scenario.find_user
108
+ end
109
+ end
110
+
111
+ should "not be persisted if User.where returns empty array" do
112
+ User.meta_mock :where, []
113
+ assert !@scenario.persisted?
114
+ end
115
+
116
+ should "be persisted if User.where returns a user" do
117
+ User.meta_mock :where, [User.new]
118
+ assert @scenario.persisted?
119
+ end
120
+
121
+ should "constantize the configured user class" do
122
+ assert_equal User, @scenario.user_class
123
+ end
124
+
125
+ should "get its name from it's factory" do
126
+ assert_equal "user", @scenario.name
127
+ end
128
+
129
+ context "#user_id" do
130
+ should "get its user_id from storage" do
131
+ @scenario.storage["user"] = 50
132
+ assert_equal 50, @scenario.user_id
133
+ end
134
+
135
+ should "not have a user_id of 0 if storage doesn't have it" do
136
+ @scenario.storage["user"] = 50
137
+ @scenario.storage.delete "user"
138
+ assert_equal 0, @scenario.user_id
139
+ end
140
+ end
141
+
142
+ context "#clear" do
143
+ should "clear the user_id for a the factory" do
144
+ @scenario.storage["name"] = 500
145
+ @scenario.clear
146
+ assert_equal 0, @scenario.user_id
147
+ end
148
+ end
149
+ end
150
+ end
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factory_scenarios
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Collin Miller
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70189401419380 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70189401419380
25
+ - !ruby/object:Gem::Dependency
26
+ name: dkastner-moneta
27
+ requirement: &70189401417080 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '0.7'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70189401417080
36
+ - !ruby/object:Gem::Dependency
37
+ name: factory_girl
38
+ requirement: &70189401414220 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '2'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70189401414220
47
+ - !ruby/object:Gem::Dependency
48
+ name: warden
49
+ requirement: &70189401412680 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70189401412680
58
+ - !ruby/object:Gem::Dependency
59
+ name: haml
60
+ requirement: &70189401434980 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '3.0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *70189401434980
69
+ - !ruby/object:Gem::Dependency
70
+ name: hashie
71
+ requirement: &70189401434360 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: '1.1'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *70189401434360
80
+ - !ruby/object:Gem::Dependency
81
+ name: shoulda
82
+ requirement: &70189401433700 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70189401433700
91
+ - !ruby/object:Gem::Dependency
92
+ name: bundler
93
+ requirement: &70189401432440 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ~>
97
+ - !ruby/object:Gem::Version
98
+ version: 1.1.rc8
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *70189401432440
102
+ - !ruby/object:Gem::Dependency
103
+ name: jeweler
104
+ requirement: &70189401431800 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.8.3
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: *70189401431800
113
+ - !ruby/object:Gem::Dependency
114
+ name: rcov
115
+ requirement: &70189401431140 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *70189401431140
124
+ - !ruby/object:Gem::Dependency
125
+ name: simplecov
126
+ requirement: &70189401430440 !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: *70189401430440
135
+ description: Build your factory and log-in in a single click. Also see what your emails
136
+ look like and enter your workflows by clickig on the links in the email
137
+ email: collintmiller@gmail.com
138
+ executables: []
139
+ extensions: []
140
+ extra_rdoc_files:
141
+ - LICENSE.txt
142
+ files:
143
+ - .document
144
+ - Gemfile
145
+ - Gemfile.lock
146
+ - LICENSE.txt
147
+ - Rakefile
148
+ - Readme.mdown
149
+ - VERSION
150
+ - app/assets/javascripts/factory_scenarios.coffee
151
+ - app/assets/stylesheets/factory_scenarios.css.sass
152
+ - app/controllers/factory_scenarios/engine_controller.rb
153
+ - app/controllers/factory_scenarios/mail_previews_controller.rb
154
+ - app/controllers/factory_scenarios/scenarios_controller.rb
155
+ - app/models/factory_scenarios/scenario.rb
156
+ - app/views/factory_scenarios/mail_previews/index.html.haml
157
+ - app/views/factory_scenarios/mail_previews/show.html.haml
158
+ - app/views/factory_scenarios/scenarios/index.html.haml
159
+ - config/routes.rb
160
+ - factory_scenarios.gemspec
161
+ - lib/factory_scenarios.rb
162
+ - lib/factory_scenarios/engine.rb
163
+ - lib/factory_scenarios/mail.rb
164
+ - lib/factory_scenarios/mail/preview.rb
165
+ - test/helper.rb
166
+ - test/test_factory_scenarios.rb
167
+ homepage: http://github.com/collin/factory_scenarios
168
+ licenses:
169
+ - MIT
170
+ post_install_message:
171
+ rdoc_options: []
172
+ require_paths:
173
+ - lib
174
+ required_ruby_version: !ruby/object:Gem::Requirement
175
+ none: false
176
+ requirements:
177
+ - - ! '>='
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ segments:
181
+ - 0
182
+ hash: 3805684085494357005
183
+ required_rubygems_version: !ruby/object:Gem::Requirement
184
+ none: false
185
+ requirements:
186
+ - - ! '>='
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ requirements: []
190
+ rubyforge_project:
191
+ rubygems_version: 1.8.10
192
+ signing_key:
193
+ specification_version: 3
194
+ summary: Use your factories to help you navigate your application state.
195
+ test_files: []