preview 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +4 -0
- data/Rakefile +20 -0
- data/install.rb +1 -0
- data/lib/preview.rb +52 -0
- data/lib/preview/preview_processor.rb +39 -0
- data/pkg/preview-0.0.1.gem +0 -0
- data/preview.gemspec +52 -0
- data/rails/init.rb +8 -0
- data/test/preview_processor_test.rb +33 -0
- data/test/preview_test.rb +116 -0
- data/test/test_helper.rb +10 -0
- data/uninstall.rb +1 -0
- metadata +81 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 [name of plugin creator]
|
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.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gemspec|
|
7
|
+
gemspec.name = "preview"
|
8
|
+
gemspec.version = "0.0.2"
|
9
|
+
gemspec.summary = "Add preview functionality to rails resources"
|
10
|
+
gemspec.description = "Make an ActiveRecord object previewable, and display the preview using existing/custom templates"
|
11
|
+
gemspec.email = "bigbeliever@gmail.com"
|
12
|
+
gemspec.homepage = "http://github.com/zerothabhishek/preview"
|
13
|
+
gemspec.authors = ["sifar"]
|
14
|
+
end
|
15
|
+
Jeweler::RubygemsDotOrgTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
18
|
+
end
|
19
|
+
|
20
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
data/lib/preview.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'preview/preview_processor'
|
2
|
+
|
3
|
+
module Preview
|
4
|
+
module Controllers
|
5
|
+
|
6
|
+
def self.included base
|
7
|
+
base.extend(Preview::Controllers::ClassMethods)
|
8
|
+
Preview::Views.modify_form_helpers
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def previewable options={}
|
13
|
+
include Preview::Controllers::InstanceMethods
|
14
|
+
self.class_eval { cattr_accessor :preview }
|
15
|
+
self.preview = PreviewProcessor.new(self, options)
|
16
|
+
self.before_filter(:do_preview, {:only => self.preview.actions })
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module InstanceMethods
|
21
|
+
def do_preview
|
22
|
+
return if params[:preview].blank?
|
23
|
+
self.class.preview.process request
|
24
|
+
render self.class.preview.template
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
module Views
|
31
|
+
def self.modify_form_helpers
|
32
|
+
ActionView::Helpers::FormBuilder.class_eval do
|
33
|
+
alias orig_submit submit
|
34
|
+
def submit value=nil, options={}
|
35
|
+
options[:onclick] = "this.form.target='';return true;"
|
36
|
+
orig_submit(value, options)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
ActionView::Helpers::FormBuilder.send(:include, Preview::Views::Helpers)
|
40
|
+
end
|
41
|
+
|
42
|
+
module Helpers
|
43
|
+
def preview options={}
|
44
|
+
options[:id] = "preview"
|
45
|
+
options[:name] = "preview"
|
46
|
+
options[:onclick] = "this.form.target='_blank';return true;"
|
47
|
+
orig_submit("preview", options)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Preview
|
2
|
+
class PreviewProcessor
|
3
|
+
|
4
|
+
attr_accessor :controller, :modelKlass, :actions, :template
|
5
|
+
|
6
|
+
def initialize controllerKlass, options={}
|
7
|
+
@controller = controllerKlass # the class name of controller
|
8
|
+
@modelKlass = model_for @controllerName # the class name of the corresponding model
|
9
|
+
@actions = options[:actions] || [:create, :update]
|
10
|
+
@template = options[:template] || "show"
|
11
|
+
end
|
12
|
+
|
13
|
+
def process request
|
14
|
+
controllerInstance = request.env["action_controller.instance"]
|
15
|
+
params = controllerInstance.params
|
16
|
+
|
17
|
+
data = params[@modelKlass.to_s.downcase]
|
18
|
+
modelObj = @modelKlass.new(data)
|
19
|
+
modelObj.id = params[:id].to_i
|
20
|
+
|
21
|
+
instanceVariable = ("@" + @modelKlass.to_s.downcase).to_sym
|
22
|
+
controllerInstance.instance_variable_set(instanceVariable, modelObj)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def model_for controllerKlass
|
28
|
+
@controller.to_s.split("Controller").first.singularize.constantize
|
29
|
+
end
|
30
|
+
|
31
|
+
def controller_from request
|
32
|
+
controllerName = request.parameters[:controller] # "posts"
|
33
|
+
controllerKlass = (controllerName.capitalize +"Controller").constantize # "posts"->"Posts"->"PostsController"->PostsController
|
34
|
+
# controllerKlass = request.env["action_controller.instance"].class
|
35
|
+
return controllerKlass
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
Binary file
|
data/preview.gemspec
ADDED
@@ -0,0 +1,52 @@
|
|
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 = %q{preview}
|
8
|
+
s.version = "0.0.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["sifar"]
|
12
|
+
s.date = %q{2011-01-14}
|
13
|
+
s.description = %q{Make an ActiveRecord object previewable, and display the preview using existing/custom templates}
|
14
|
+
s.email = %q{bigbeliever@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"MIT-LICENSE",
|
20
|
+
"README.rdoc",
|
21
|
+
"Rakefile",
|
22
|
+
"install.rb",
|
23
|
+
"lib/preview.rb",
|
24
|
+
"lib/preview/preview_processor.rb",
|
25
|
+
"pkg/preview-0.0.1.gem",
|
26
|
+
"preview.gemspec",
|
27
|
+
"rails/init.rb",
|
28
|
+
"test/preview_processor_test.rb",
|
29
|
+
"test/preview_test.rb",
|
30
|
+
"test/test_helper.rb",
|
31
|
+
"uninstall.rb"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/zerothabhishek/preview}
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.4.1}
|
36
|
+
s.summary = %q{Add preview functionality to rails resources}
|
37
|
+
s.test_files = [
|
38
|
+
"test/preview_processor_test.rb",
|
39
|
+
"test/preview_test.rb",
|
40
|
+
"test/test_helper.rb"
|
41
|
+
]
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
+
else
|
48
|
+
end
|
49
|
+
else
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
data/rails/init.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "lib", "preview")
|
2
|
+
|
3
|
+
# Ask ActiveRecord::Base to include code in the Preview::Models module
|
4
|
+
# Execute the ActiveRecord::Base.include method with Preview::Models arg
|
5
|
+
#ActiveRecord::Base.send(:include, Preview::Models)
|
6
|
+
|
7
|
+
# Ask ActionController::Base to include Preivew::Controller::InstanceMethods
|
8
|
+
ActionController::Base.send(:include, Preview::Controllers)
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'mocha'
|
3
|
+
|
4
|
+
class FoosController < ActionController::Base ; end
|
5
|
+
class Foo; end
|
6
|
+
class FakeRequest
|
7
|
+
def env
|
8
|
+
fc = FoosController.new
|
9
|
+
{"action_controller.instance" => fc}
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class PreviewProcessorTest < ActiveSupport::TestCase
|
14
|
+
|
15
|
+
test "initialize should initialize the attributes controller, modelKlass, actions and template" do
|
16
|
+
p = Preview::PreviewProcessor.new FoosController
|
17
|
+
assert FoosController, p.controller
|
18
|
+
assert Foo, p.modelKlass
|
19
|
+
assert [:create, :update], p.actions
|
20
|
+
assert "show", p.template
|
21
|
+
end
|
22
|
+
|
23
|
+
test "process should set an instance variable by the name of the model for the controller instance" do
|
24
|
+
p = Preview::PreviewProcessor.new FoosController
|
25
|
+
request = FakeRequest.new
|
26
|
+
FoosController.any_instance.stubs(:params).returns(:Foo => {})
|
27
|
+
Foo.stubs(:new).returns(Foo.new)
|
28
|
+
|
29
|
+
p.process request
|
30
|
+
assert p.instance_variable_defined?(:@foo)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'mocha'
|
3
|
+
|
4
|
+
class TestController < ActionController::Base ; end
|
5
|
+
|
6
|
+
class PreviewTest < ActiveSupport::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
end
|
10
|
+
|
11
|
+
# previewable
|
12
|
+
test "the class method _previewable_ should be available to all controllers in project using the gem" do
|
13
|
+
assert ActionController::Base.respond_to?"previewable"
|
14
|
+
end
|
15
|
+
|
16
|
+
test "a controller using _previewable_ declaration should get the *do_preview* instance method" do
|
17
|
+
TestController.previewable
|
18
|
+
assert TestController.method_defined? :do_preview
|
19
|
+
end
|
20
|
+
|
21
|
+
test "a controller using _previewable_ declaration should get the class attribute _preview_" do
|
22
|
+
TestController.previewable
|
23
|
+
assert TestController.class_variable_defined?(:@@preview)
|
24
|
+
assert TestController.respond_to? :preview
|
25
|
+
assert TestController.respond_to? :preview=
|
26
|
+
end
|
27
|
+
|
28
|
+
test "a controller using _previewable_ should have the preview attribute holding a PreviewProcessor object" do
|
29
|
+
TestController.previewable
|
30
|
+
assert_equal Preview::PreviewProcessor, TestController.preview.class
|
31
|
+
end
|
32
|
+
|
33
|
+
test "controller using _previewable_ should get a do_preview before_filter for the specified actions" do
|
34
|
+
TestController.expects(:before_filter).with(:do_preview, {:only => :create})
|
35
|
+
TestController.previewable :actions=>:create
|
36
|
+
end
|
37
|
+
|
38
|
+
# do_preview
|
39
|
+
test "do_preview should not process preview if the preview parameter is not set" do
|
40
|
+
TestController.any_instance.stubs(:params).returns({:preview => nil})
|
41
|
+
Preview::PreviewProcessor.expects(:process).never
|
42
|
+
TestController.previewable
|
43
|
+
assert_equal nil, TestController.new.do_preview
|
44
|
+
end
|
45
|
+
|
46
|
+
test "do_preview should process preview if preview parameter is set" do
|
47
|
+
TestController.any_instance.stubs(:params).returns({:preview => true})
|
48
|
+
TestController.any_instance.stubs(:request).returns("requestHash")
|
49
|
+
Preview::PreviewProcessor.any_instance.stubs(:process).returns(true)
|
50
|
+
|
51
|
+
TestController.any_instance.expects(:render).with("show").returns(true)
|
52
|
+
Preview::PreviewProcessor.any_instance.expects(:process).with("requestHash").once
|
53
|
+
|
54
|
+
TestController.previewable
|
55
|
+
TestController.new.do_preview
|
56
|
+
end
|
57
|
+
|
58
|
+
# form helpers
|
59
|
+
test "a *preview* form builder method should be available for a project using the gem" do
|
60
|
+
assert ActionView::Helpers::FormBuilder.method_defined? :preview
|
61
|
+
end
|
62
|
+
|
63
|
+
test "the *submit* form builder method should be aliased as *orig_submit* for a project using the gem" do
|
64
|
+
assert ActionView::Helpers::FormBuilder.method_defined? :orig_submit
|
65
|
+
end
|
66
|
+
|
67
|
+
test "the *submit* form builder method should be modified so it sets an onclick JS option" do
|
68
|
+
assert true
|
69
|
+
end
|
70
|
+
|
71
|
+
=begin
|
72
|
+
test "previewable should include the Preview::Controllers::InstanceMethods module in the calling class" do
|
73
|
+
# stub the other functionality
|
74
|
+
TestClass.stubs(:class_eval).returns(true)
|
75
|
+
TestClass.stubs(:before_filter).returns(true)
|
76
|
+
TestClass.stubs(:preview=).returns(true)
|
77
|
+
Preview::PreviewProcessor.stubs(:new).returns(true)
|
78
|
+
|
79
|
+
TestClass.expects(:include).with(Preview::Controllers::InstanceMethods).once
|
80
|
+
TestClass.previewable
|
81
|
+
end
|
82
|
+
|
83
|
+
test "previewable should add the preview attribute to the calling class" do
|
84
|
+
# stub the other functionality
|
85
|
+
TestClass.stubs(:before_filter).returns(true)
|
86
|
+
TestClass.stubs(:include).returns(true)
|
87
|
+
PreviewProcessor.stubs(:new).returns("foo")
|
88
|
+
|
89
|
+
TestClass.previewable
|
90
|
+
assert_equal "foo", TestClass.preview
|
91
|
+
assert_nothing_raised(NoMethodError){ TestClass.preview = "hello" }
|
92
|
+
end
|
93
|
+
|
94
|
+
test "previewable should assign a PreviewProcessor object to the class attribute preview " do
|
95
|
+
# stub out the other functionality
|
96
|
+
TestClass.stubs(:before_filter).returns(true)
|
97
|
+
TestClass.stubs(:include).returns(true)
|
98
|
+
PreviewProcessor.stubs(:new).returns("preview_processor_obj")
|
99
|
+
|
100
|
+
TestClass.previewable
|
101
|
+
assert_equal "preview_processor_obj", TestClass.preview
|
102
|
+
end
|
103
|
+
|
104
|
+
test "previewable should invoke the before_filter for the calling class" do
|
105
|
+
# stub the other functionality
|
106
|
+
TestClass.stubs(:include).returns(true)
|
107
|
+
TestClass.stubs(:class_eval).returns(true)
|
108
|
+
TestClass.stubs(:preview=).returns(true)
|
109
|
+
PreviewProcessor.stubs(:new).returns(true)
|
110
|
+
|
111
|
+
TestClass.expects(:before_filter).with(:do_preview).once
|
112
|
+
TestClass.previewable
|
113
|
+
end
|
114
|
+
=end
|
115
|
+
|
116
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'active_support'
|
4
|
+
require 'action_controller'
|
5
|
+
|
6
|
+
gem_root = File.join(File.dirname(__FILE__), '..')
|
7
|
+
$LOAD_PATH << File.join(gem_root, 'lib')
|
8
|
+
$LOAD_PATH << File.join(gem_root, 'lib', 'preview')
|
9
|
+
|
10
|
+
require File.join(gem_root, 'init.rb')
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: preview
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- sifar
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-14 00:00:00 +05:30
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Make an ActiveRecord object previewable, and display the preview using existing/custom templates
|
23
|
+
email: bigbeliever@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- MIT-LICENSE
|
32
|
+
- README.rdoc
|
33
|
+
- Rakefile
|
34
|
+
- install.rb
|
35
|
+
- lib/preview.rb
|
36
|
+
- lib/preview/preview_processor.rb
|
37
|
+
- pkg/preview-0.0.1.gem
|
38
|
+
- preview.gemspec
|
39
|
+
- rails/init.rb
|
40
|
+
- test/preview_processor_test.rb
|
41
|
+
- test/preview_test.rb
|
42
|
+
- test/test_helper.rb
|
43
|
+
- uninstall.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/zerothabhishek/preview
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.4.1
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Add preview functionality to rails resources
|
78
|
+
test_files:
|
79
|
+
- test/preview_processor_test.rb
|
80
|
+
- test/preview_test.rb
|
81
|
+
- test/test_helper.rb
|