webmachine-actionview 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +98 -0
- data/Rakefile +1 -0
- data/lib/webmachine/actionview.rb +4 -0
- data/lib/webmachine/actionview/configuration.rb +21 -0
- data/lib/webmachine/actionview/resource.rb +50 -0
- data/lib/webmachine/actionview/version.rb +5 -0
- data/spec/dummy/app.rb +11 -0
- data/spec/dummy/resources/home_resource.rb +15 -0
- data/spec/dummy/views/foo.html.unsupportedfiletype +0 -0
- data/spec/dummy/views/home.html.erb +4 -0
- data/spec/dummy/views/layouts/application.html.erb +4 -0
- data/spec/dummy/views/layouts/explicit.html.erb +1 -0
- data/spec/dummy/views/shared/_some_partial.html.erb +1 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/webmachine/actionview/configuration_spec.rb +20 -0
- data/spec/webmachine/actionview/resource_spec.rb +103 -0
- data/webmachine-actionview.gemspec +25 -0
- metadata +131 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Russell Garner
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# Webmachine::Actionview
|
2
|
+
|
3
|
+
Integration of some Rails-style view conventions into Webmachine. Uses your resource as a view context.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'webmachine-actionview'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install webmachine-actionview
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Include `Webmachine::ActionView::Resource` in resources you want to use ActionView. Somewhere in your app's
|
22
|
+
startup, you'll need to tell it where views live and (optionally) what handlers you're supporting, for example:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
Webmachine::ActionView.configure do |config|
|
26
|
+
config.view_paths = [MY_VIEWS_PATH]
|
27
|
+
config.handlers = [:erb, :haml, :builder]
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
In your resource, you'll need to render the view at some point. Rendering is broadly similar to that in Rails.
|
32
|
+
The simplest thing you can do is this:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class HomeResource < Webmachine::Resource
|
36
|
+
include Webmachine::ActionView::Resource
|
37
|
+
def to_html
|
38
|
+
render
|
39
|
+
end
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
This will look for a template called "home" in your `Webmachine::ActionView.configuration.view_paths`. By default, this
|
44
|
+
will look for an application layout of `layouts/application`.
|
45
|
+
|
46
|
+
You can be more specific:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
render template: 'other_template', layout: 'mini'
|
50
|
+
```
|
51
|
+
|
52
|
+
You can suppress a layout:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
render layout: nil
|
56
|
+
# or
|
57
|
+
render layout: false
|
58
|
+
```
|
59
|
+
|
60
|
+
Your resource itself is used as the view context. This means that you can use instance variables as you would in Rails.
|
61
|
+
For example:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
class Customer << Webmachine::Resource
|
65
|
+
include Webmachine::ActionView::Resource
|
66
|
+
|
67
|
+
def resource_exists?
|
68
|
+
@customer = Customer.find(some_id)
|
69
|
+
end
|
70
|
+
|
71
|
+
def to_html
|
72
|
+
render
|
73
|
+
end
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
And in an associated `customer.html.erb` template, `@customer` will be available:
|
78
|
+
|
79
|
+
```erb
|
80
|
+
<p class="name"><%= @customer.name %></p>
|
81
|
+
```
|
82
|
+
|
83
|
+
You can also render partials from your views:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
render partial: 'shared/my_partial'
|
87
|
+
```
|
88
|
+
|
89
|
+
Partials are subject to the same conventions as Rails, i.e. their filenames begin with an underscore, but no underscore
|
90
|
+
is required when making a call to `render partial: 'some_partial'`.
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
1. Fork it
|
95
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
96
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
97
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
98
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'active_support/configurable'
|
2
|
+
|
3
|
+
module Webmachine
|
4
|
+
module ActionView
|
5
|
+
|
6
|
+
# Configures settings for Webmachine::ActionView
|
7
|
+
def self.configure(&block)
|
8
|
+
yield @config ||= Configuration.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.config
|
12
|
+
@config
|
13
|
+
end
|
14
|
+
|
15
|
+
class Configuration #:nodoc:
|
16
|
+
include ActiveSupport::Configurable
|
17
|
+
|
18
|
+
config_accessor :view_paths, :handlers
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Webmachine
|
2
|
+
module ActionView
|
3
|
+
def self.view_paths
|
4
|
+
[]
|
5
|
+
end
|
6
|
+
|
7
|
+
module Resource
|
8
|
+
def self.included(base)
|
9
|
+
base.class_eval do
|
10
|
+
include ::ActionView::Helpers::CaptureHelper
|
11
|
+
include ::ActionView::Context
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
DEFAULT_LAYOUT = 'layouts/application'
|
16
|
+
|
17
|
+
def render(options = nil)
|
18
|
+
options ||= default_template_name
|
19
|
+
options = { :template => options } if options.is_a?(String)
|
20
|
+
|
21
|
+
unless options[:partial]
|
22
|
+
if options.has_key?(:layout) && !options[:layout]
|
23
|
+
options.delete(:layout)
|
24
|
+
else
|
25
|
+
options.reverse_merge!(layout: DEFAULT_LAYOUT)
|
26
|
+
end
|
27
|
+
_prepare_context
|
28
|
+
end
|
29
|
+
|
30
|
+
view_renderer.render(self, options)
|
31
|
+
end
|
32
|
+
|
33
|
+
def default_template_name
|
34
|
+
@default_template_name ||= self.class.name.underscore.sub(/_resource$/, '')
|
35
|
+
end
|
36
|
+
|
37
|
+
def view_paths
|
38
|
+
@view_paths ||= ::ActionView::PathSet.new(Webmachine::ActionView.config.view_paths)
|
39
|
+
end
|
40
|
+
|
41
|
+
def lookup_context
|
42
|
+
@lookup_context ||= ::ActionView::LookupContext.new(view_paths)
|
43
|
+
end
|
44
|
+
|
45
|
+
def view_renderer
|
46
|
+
@view_renderer ||= ::ActionView::Renderer.new(lookup_context)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/spec/dummy/app.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
class HomeResource < Webmachine::Resource
|
2
|
+
include Webmachine::ActionView::Resource
|
3
|
+
|
4
|
+
def to_html
|
5
|
+
@home = HomeResource.home_instance
|
6
|
+
render
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.home_instance
|
10
|
+
Struct.new(:id, :name).new.tap do |h|
|
11
|
+
h.id = 1
|
12
|
+
h.name = "some name"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
FUSKING CLOFF PRUNKER
|
@@ -0,0 +1 @@
|
|
1
|
+
<p>content from shared/some_partial</p>
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Webmachine::ActionView::Configuration do
|
4
|
+
describe "Configuring" do
|
5
|
+
let(:paths) { ['somewhere/over/the/rainbow'] }
|
6
|
+
let(:handlers) { ['erb', 'slim', 'haml'] }
|
7
|
+
|
8
|
+
before do
|
9
|
+
Webmachine::ActionView.configure do |c|
|
10
|
+
c.view_paths = paths
|
11
|
+
c.handlers = handlers
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
subject(:config) { Webmachine::ActionView.config }
|
16
|
+
|
17
|
+
its(:view_paths) { should eql(paths) }
|
18
|
+
its(:handlers) { should eql(handlers) }
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
class BaseResource < Webmachine::Resource
|
4
|
+
include Webmachine::ActionView::Resource
|
5
|
+
end
|
6
|
+
|
7
|
+
class UnconventionalName < BaseResource
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "A resource including Webmachine::ActionView::Resource" do
|
11
|
+
let(:request) { nil }
|
12
|
+
let(:response) { nil }
|
13
|
+
|
14
|
+
let(:resource) { HomeResource.new(request, response) }
|
15
|
+
let(:unconventional_resource) { UnconventionalName.new(request, response) }
|
16
|
+
|
17
|
+
before :all do
|
18
|
+
Webmachine::ActionView.configure do |c|
|
19
|
+
c.view_paths = ['spec/dummy/views']
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "Default places to look for templates" do
|
24
|
+
describe "the path set" do
|
25
|
+
subject(:view_paths) { resource.view_paths }
|
26
|
+
|
27
|
+
it { should be_an(ActionView::PathSet) }
|
28
|
+
it "has the path we previously configured" do
|
29
|
+
view_paths.paths.map(&:to_s).should include(File.expand_path('spec/dummy/views'))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "A conventionally-named resource" do
|
34
|
+
it "looks for a template without resource in the class name" do
|
35
|
+
resource.default_template_name.should == 'home'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
context "An unconventionally-named resource" do
|
39
|
+
it "looks for a template as an underscored version of its class name" do
|
40
|
+
unconventional_resource.default_template_name.should == 'unconventional_name'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "Finding templates through the lookup context" do
|
46
|
+
subject(:lookup_context) { resource.lookup_context }
|
47
|
+
|
48
|
+
it { should be_an(ActionView::LookupContext) }
|
49
|
+
|
50
|
+
it "finds templates implicitly" do
|
51
|
+
lookup_context.find('home').should be_a(ActionView::Template)
|
52
|
+
end
|
53
|
+
it "finds templates implicitly when html is mentioned" do
|
54
|
+
lookup_context.find('home.html').should be_a(ActionView::Template)
|
55
|
+
end
|
56
|
+
it "fails to find templates for unmentioned extensions" do
|
57
|
+
lambda { resource.lookup_context.find('foo') }.should raise_error(ActionView::MissingTemplate)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "Rendering from a template with a context" do
|
62
|
+
subject(:rendered) { resource.to_html }
|
63
|
+
|
64
|
+
it { should include('id: 1') }
|
65
|
+
it { should include('name: some name') }
|
66
|
+
|
67
|
+
it { should include('content from the application layout')}
|
68
|
+
it { should include('content from shared/some_partial')}
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "Explicit template rendering" do
|
72
|
+
subject(:rendered) { resource.to_html }
|
73
|
+
|
74
|
+
context "With an explicit layout" do
|
75
|
+
let(:resource) do
|
76
|
+
HomeResource.new(request, response).tap do |h|
|
77
|
+
def h.to_html
|
78
|
+
@home = HomeResource.home_instance
|
79
|
+
render template: 'home', layout: 'layouts/explicit'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should include explicit content" do
|
85
|
+
rendered.should include('FUSKING CLOFF PRUNKER')
|
86
|
+
end
|
87
|
+
end
|
88
|
+
context "Suppressing default layout" do
|
89
|
+
let(:resource) do
|
90
|
+
HomeResource.new(request, response).tap do |h|
|
91
|
+
def h.to_html
|
92
|
+
@home = HomeResource.home_instance
|
93
|
+
render template: 'home', layout: false
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should not include explicit content" do
|
99
|
+
rendered.should_not include('FUSKING CLOFF PRUNKER')
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'webmachine/actionview/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "webmachine-actionview"
|
8
|
+
gem.version = Webmachine::ActionView::VERSION
|
9
|
+
gem.authors = ["Russell Garner"]
|
10
|
+
gem.email = ["rgarner@zephyros-systems.co.uk"]
|
11
|
+
gem.description = %q{Brings ActionView to webmachine-ruby}
|
12
|
+
gem.summary = %q{Want the Rails conventions that you're used to for HTML views in webmachine-ruby? This is your gem.}
|
13
|
+
gem.homepage = "http://github.com/rgarner/webmachine-actionview"
|
14
|
+
|
15
|
+
gem.add_development_dependency "rspec"
|
16
|
+
gem.add_development_dependency "webmachine-test"
|
17
|
+
|
18
|
+
gem.add_runtime_dependency "webmachine"
|
19
|
+
gem.add_runtime_dependency "activesupport"
|
20
|
+
gem.add_runtime_dependency "actionpack"
|
21
|
+
|
22
|
+
gem.files = `git ls-files`.split($/)
|
23
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
24
|
+
gem.require_paths = ["lib"]
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: webmachine-actionview
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Russell Garner
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &2152313240 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2152313240
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: webmachine-test
|
27
|
+
requirement: &2152312620 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2152312620
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: webmachine
|
38
|
+
requirement: &2152311940 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2152311940
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: activesupport
|
49
|
+
requirement: &2152311380 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2152311380
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: actionpack
|
60
|
+
requirement: &2152310840 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :runtime
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *2152310840
|
69
|
+
description: Brings ActionView to webmachine-ruby
|
70
|
+
email:
|
71
|
+
- rgarner@zephyros-systems.co.uk
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/webmachine/actionview.rb
|
82
|
+
- lib/webmachine/actionview/configuration.rb
|
83
|
+
- lib/webmachine/actionview/resource.rb
|
84
|
+
- lib/webmachine/actionview/version.rb
|
85
|
+
- spec/dummy/app.rb
|
86
|
+
- spec/dummy/resources/home_resource.rb
|
87
|
+
- spec/dummy/views/foo.html.unsupportedfiletype
|
88
|
+
- spec/dummy/views/home.html.erb
|
89
|
+
- spec/dummy/views/layouts/application.html.erb
|
90
|
+
- spec/dummy/views/layouts/explicit.html.erb
|
91
|
+
- spec/dummy/views/shared/_some_partial.html.erb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
- spec/webmachine/actionview/configuration_spec.rb
|
94
|
+
- spec/webmachine/actionview/resource_spec.rb
|
95
|
+
- webmachine-actionview.gemspec
|
96
|
+
homepage: http://github.com/rgarner/webmachine-actionview
|
97
|
+
licenses: []
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.8.16
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Want the Rails conventions that you're used to for HTML views in webmachine-ruby?
|
120
|
+
This is your gem.
|
121
|
+
test_files:
|
122
|
+
- spec/dummy/app.rb
|
123
|
+
- spec/dummy/resources/home_resource.rb
|
124
|
+
- spec/dummy/views/foo.html.unsupportedfiletype
|
125
|
+
- spec/dummy/views/home.html.erb
|
126
|
+
- spec/dummy/views/layouts/application.html.erb
|
127
|
+
- spec/dummy/views/layouts/explicit.html.erb
|
128
|
+
- spec/dummy/views/shared/_some_partial.html.erb
|
129
|
+
- spec/spec_helper.rb
|
130
|
+
- spec/webmachine/actionview/configuration_spec.rb
|
131
|
+
- spec/webmachine/actionview/resource_spec.rb
|