peek-a-view 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/.gitignore +14 -0
  2. data/Gemfile +14 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +59 -0
  5. data/Rakefile +38 -0
  6. data/app/controllers/peek_a_view/views_controller.rb +80 -0
  7. data/app/views/peek_a_view/views/index.html.erb +15 -0
  8. data/config/routes.rb +6 -0
  9. data/lib/peek-a-view.rb +1 -0
  10. data/lib/peek_a_view.rb +30 -0
  11. data/lib/peek_a_view/configuration.rb +48 -0
  12. data/lib/peek_a_view/engine.rb +7 -0
  13. data/lib/peek_a_view/version.rb +3 -0
  14. data/lib/peek_a_view/view.rb +43 -0
  15. data/lib/tasks/views.rake +9 -0
  16. data/peek-a-view.gemspec +19 -0
  17. data/script/rails +8 -0
  18. data/test/dummy/README.rdoc +261 -0
  19. data/test/dummy/Rakefile +7 -0
  20. data/test/dummy/app/assets/javascripts/application.js +13 -0
  21. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  22. data/test/dummy/app/controllers/application_controller.rb +3 -0
  23. data/test/dummy/app/helpers/application_helper.rb +2 -0
  24. data/test/dummy/app/mailers/.gitkeep +0 -0
  25. data/test/dummy/app/models/.gitkeep +0 -0
  26. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  27. data/test/dummy/config.ru +4 -0
  28. data/test/dummy/config/application.rb +65 -0
  29. data/test/dummy/config/boot.rb +10 -0
  30. data/test/dummy/config/environment.rb +5 -0
  31. data/test/dummy/config/environments/development.rb +31 -0
  32. data/test/dummy/config/environments/production.rb +64 -0
  33. data/test/dummy/config/environments/test.rb +35 -0
  34. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  35. data/test/dummy/config/initializers/inflections.rb +15 -0
  36. data/test/dummy/config/initializers/mime_types.rb +5 -0
  37. data/test/dummy/config/initializers/secret_token.rb +7 -0
  38. data/test/dummy/config/initializers/session_store.rb +8 -0
  39. data/test/dummy/config/initializers/wrap_parameters.rb +10 -0
  40. data/test/dummy/config/locales/en.yml +5 -0
  41. data/test/dummy/config/routes.rb +4 -0
  42. data/test/dummy/lib/assets/.gitkeep +0 -0
  43. data/test/dummy/log/.gitkeep +0 -0
  44. data/test/dummy/public/404.html +26 -0
  45. data/test/dummy/public/422.html +26 -0
  46. data/test/dummy/public/500.html +25 -0
  47. data/test/dummy/public/favicon.ico +0 -0
  48. data/test/dummy/script/rails +6 -0
  49. data/test/integration/navigation_test.rb +9 -0
  50. data/test/peek-a-view_test.rb +7 -0
  51. data/test/test_helper.rb +15 -0
  52. metadata +153 -0
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ *~
2
+ *.orig
3
+ *.bak
4
+ .rvmrc
5
+ .idea
6
+ .bundle/
7
+ .rake_t_cache
8
+ log/*.log
9
+ pkg/
10
+ Gemfile.lock
11
+ test/dummy/db/*.sqlite3
12
+ test/dummy/log/*.log
13
+ test/dummy/tmp/
14
+ test/dummy/.sass-cache
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in peek-a-view.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use debugger
14
+ # gem 'debugger'
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Michael Schuerig, <michael@schuerig.de>
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/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # PeekAView
2
+
3
+ Experimental software ahead! Use with caution, if at all.
4
+
5
+ ## What?
6
+
7
+ This gem provides a Rails engine adds functionality to an application
8
+ to show any of its views with stubbed data.
9
+
10
+ ## Why?
11
+
12
+ To get to a certain page in your application you may have to jump
13
+ through more hoops than is convenient. Or it might be complicated to
14
+ set up the necessary data. Or it may require external services to do
15
+ something interesting. Or you want to run a validator on all your
16
+ pages without being bothered by a login.
17
+
18
+ ## How?
19
+
20
+ Declare a dependency on this gem in your Gemfile
21
+
22
+ gem 'peek-a-view'
23
+
24
+ Mount the PeekAView engine in config/routes.rb
25
+
26
+ if Rails.env.development? || Rails.env.test?
27
+ mount PeekAView::Engine => '/peek-a-view'
28
+ end
29
+
30
+ Write view definitions in {spec|test}/peek_a_view.rb like this
31
+
32
+ PeekAView.configure do |config|
33
+ # Define stubbing methods or use the ones you already have for your tests.
34
+ def stub_article
35
+ ...
36
+ end
37
+
38
+ config.all_views do |v|
39
+ v.current_user = User.new
40
+ end
41
+
42
+ config.view 'articles/index' do |v|
43
+ v.articles = (1..10).map { |i| stub_article }
44
+ end
45
+
46
+ config.view 'articles/new', 'articles/edit' do |v|
47
+ v.params = { id: '1' } # needed for URL generation
48
+ v.article = stub_article
49
+ end
50
+ end
51
+
52
+ Start your rails application and point your browser at
53
+
54
+ http://localhost:3000/peek-a-view/
55
+
56
+ If everything went well, you see a list of links to your views.
57
+
58
+
59
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'PeekAView'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,80 @@
1
+ module PeekAView
2
+ class ViewsController < ActionController::Base
3
+
4
+ def index
5
+ PeekAView.load_stubs
6
+ @views = config.views.keys.sort
7
+ render template: '/peek_a_view/views/index'
8
+ end
9
+
10
+ def show
11
+ PeekAView.load_stubs
12
+
13
+ if (view = find_view)
14
+ render_template(view, params)
15
+ else
16
+ render text: "No such template", status: :not_found
17
+ end
18
+ end
19
+
20
+
21
+ def url_options
22
+ # Substitute stubbed params for URL generation.
23
+ { _path_segments: @params }
24
+ end
25
+
26
+ hide_action :url_options
27
+
28
+ private
29
+
30
+ def config
31
+ PeekAView::Engine.config.peek_a_view
32
+ end
33
+
34
+ def find_view
35
+ config.views[params[:view]]
36
+ end
37
+
38
+ def render_template(view, params)
39
+ flash[:alert] = params[:flash_alert]
40
+ flash[:notice] = params[:flash_notice]
41
+
42
+ template = view.template
43
+
44
+ stub_instance_variables_for!(view, variant: params[:variant])
45
+ render template: template, layout: find_layout(view)
46
+ end
47
+
48
+ def _prefixes
49
+ return super unless params[:action] == 'show'
50
+ # Fake the path prefix for partials specified only with a relative path.
51
+ # As we are not rendering them with their expected controller, we need
52
+ # to make sure they can be found.
53
+ [File.dirname(params[:view])]
54
+ end
55
+
56
+ def find_layout(view)
57
+ layout = params[:layout]
58
+ case layout
59
+ when 'true', true
60
+ 'application'
61
+ when 'false', false
62
+ false
63
+ when nil
64
+ view.layout
65
+ else
66
+ layout
67
+ end
68
+ end
69
+
70
+ def stub_instance_variables_for!(view, options)
71
+ view.variables(options).each do |attr, value|
72
+ instance_variable_set("@#{attr}", value)
73
+ self.singleton_class.tap do |c|
74
+ c.send(:attr_reader, attr) unless c.method_defined?(attr) # Don't overwrite an existing method.
75
+ c.send(:helper_method, attr)
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Peek-a-View</title>
5
+ </head>
6
+ <body>
7
+ <ul>
8
+ <% @views.each do |v| %>
9
+ <li>
10
+ <%= link_to v, peek_a_view_engine.view_path(v) %>
11
+ </li>
12
+ <% end %>
13
+ </ul>
14
+ </body>
15
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,6 @@
1
+ PeekAView::Engine.routes.draw do
2
+ scope module: 'peek_a_view' do
3
+ get '/' => 'views#index', as: 'views'
4
+ get '/*view' => 'views#show', as: 'view'
5
+ end
6
+ end
@@ -0,0 +1 @@
1
+ require 'peek_a_view'
@@ -0,0 +1,30 @@
1
+ require 'peek_a_view/engine'
2
+ require 'peek_a_view/configuration'
3
+
4
+ module PeekAView
5
+ STUBS_FILE = 'peek_a_view.rb'
6
+
7
+ class << self
8
+
9
+ def configure # :yields: config
10
+ yield config
11
+ end
12
+
13
+ def load_stubs
14
+ config.load_stubs
15
+ end
16
+
17
+ private
18
+
19
+ def config
20
+ PeekAView::Engine.config.peek_a_view
21
+ end
22
+ end
23
+ end
24
+
25
+ PeekAView.configure do |c|
26
+ c.stubs_path = [
27
+ File.join('spec', PeekAView::STUBS_FILE),
28
+ File.join('test', PeekAView::STUBS_FILE)
29
+ ]
30
+ end
@@ -0,0 +1,48 @@
1
+ require 'active_support/ordered_options'
2
+ require 'peek_a_view/view'
3
+
4
+ module PeekAView
5
+ class Configuration < ActiveSupport::OrderedOptions
6
+ def initialize(*)
7
+ super
8
+ clear_stubs!
9
+ end
10
+
11
+ def stubs_file
12
+ Array(stubs_path).map { |p| File.join(Rails.root, p) }.find { |p| File.file?(p) }
13
+ end
14
+
15
+ def load_stubs
16
+ clear_stubs!
17
+ if (file = stubs_file)
18
+ load file
19
+ else
20
+ raise "No peek-a-view definitions found." # TODO proper exception class
21
+ end
22
+ end
23
+
24
+ def clear_stubs!
25
+ @views = { }
26
+ @common = []
27
+ end
28
+
29
+ def view(*names, &block) # :yields: a view object
30
+ raise ArgumentError, "A view must have a least one name." unless names.length > 0
31
+ names.each do |name|
32
+ (@views[name] ||= PeekAView::View.new(name, @common)).tap do |view|
33
+ view.record(block)
34
+ end
35
+ end
36
+ end
37
+
38
+ def all_views(&block) # :yields: a common view object
39
+ @common.tap do |common|
40
+ @common << block
41
+ end
42
+ end
43
+
44
+ def views
45
+ @views
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,7 @@
1
+ require 'peek_a_view/configuration'
2
+
3
+ module PeekAView
4
+ class Engine < ::Rails::Engine
5
+ config.peek_a_view = PeekAView::Configuration.new
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module PeekAView
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,43 @@
1
+ module PeekAView
2
+ class View < ActiveSupport::OrderedOptions
3
+ def initialize(name, common_block = nil)
4
+ @name = name
5
+ self.params = nil # just to initialize params
6
+ @blocks = []
7
+ record(common_block) if common_block
8
+ end
9
+
10
+ def record(block)
11
+ @blocks << block
12
+ end
13
+
14
+ def template
15
+ super || @name
16
+ end
17
+
18
+ def layout
19
+ 'application' # TODO be smarter
20
+ end
21
+
22
+ def params=(new_params)
23
+ *controller, action = template.split('/')
24
+ augmented = (new_params || { }).reverse_merge(
25
+ controller: controller.join('/'),
26
+ action: action
27
+ )
28
+ super(augmented)
29
+ augmented
30
+ end
31
+
32
+ def fixture(name)
33
+ fixture_dir = Rails.root + 'spec/fixtures/peek_a_view' # TODO config
34
+ file = File.join(fixture_dir, name)
35
+ File.read(file)
36
+ end
37
+
38
+ def variables(options = {})
39
+ @blocks.flatten.each { |block| block.call(self, options) }
40
+ self
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,9 @@
1
+ namespace :views do
2
+
3
+ #desc "Show a view in a browser (options: TEMPLATE, LAYOUT, FLASH_NOTICE, FLASH_ALERT, BROWSER, PORT)"
4
+ #task :show => :environment do
5
+ # # FIXME Is there a way to avoid this?
6
+ # Dir[Rails.root + "app/models/**/*.rb"].each { |f| require f }
7
+ #
8
+ #end
9
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/peek_a_view/version', __FILE__)
3
+
4
+ # Describe your gem and declare its dependencies:
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "peek-a-view"
7
+ gem.version = PeekAView::VERSION
8
+ gem.authors = ["Michael Schuerig"]
9
+ gem.email = ["michael@schuerig.de"]
10
+ gem.homepage = ""
11
+ gem.summary = "Look at your Rails views without bothering the application"
12
+ gem.description = "Show views using stubbed data."
13
+ gem.license = 'MIT'
14
+
15
+ gem.files = `git ls-files`.split($\)
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+
18
+ gem.add_runtime_dependency "rails", "~> 3.2.6"
19
+ end
data/script/rails ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby1.9.1
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
5
+ ENGINE_PATH = File.expand_path('../../lib/peek-a-view/engine', __FILE__)
6
+
7
+ require 'rails/all'
8
+ require 'rails/engine/commands'