easy_partials 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/LICENSE +1 -1
- data/README.rdoc +44 -5
- data/Rakefile +1 -1
- data/TODO +0 -0
- data/VERSION +1 -1
- data/easy_partials.gemspec +58 -0
- data/lib/easy_partials.rb +163 -0
- metadata +8 -5
data/.gitignore
CHANGED
data/LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -1,6 +1,49 @@
|
|
1
1
|
= easy_partials
|
2
2
|
|
3
|
-
|
3
|
+
EasyPartials are a way to make partials in Rails even easier! This is
|
4
|
+
an expansion of a blog post by Mike Stone at
|
5
|
+
"http://smellsblue.blogspot.com/2009/11/easy-partials-in-rails.html".
|
6
|
+
|
7
|
+
== FEATURES:
|
8
|
+
|
9
|
+
* Allows easy partial invocation syntax, with simpler local variable passing
|
10
|
+
* Allows configurable shared directories for partials
|
11
|
+
|
12
|
+
== SYNOPSIS:
|
13
|
+
|
14
|
+
<% _my_partial :var => "123" do %>
|
15
|
+
<p>
|
16
|
+
Some block content.
|
17
|
+
</p>
|
18
|
+
<% end %>
|
19
|
+
|
20
|
+
The above would render a partial (as with <%= render :partial =>
|
21
|
+
"my_partial" %>), with the local variable "var" set to "123", and the
|
22
|
+
local variable "body" set to the paragraph "some block content", HTML
|
23
|
+
tags included.
|
24
|
+
|
25
|
+
Note that you need to use <% rather than <%=.
|
26
|
+
|
27
|
+
For a shared partial, use a line like the following in environment.rb
|
28
|
+
or in a file under initializers:
|
29
|
+
|
30
|
+
Easypartials.shared_directories = ["mydir1", "mydir2"]
|
31
|
+
|
32
|
+
This will set up app/views/mydir1 and app/views/mydir2 as the
|
33
|
+
locations to look for partials when a directory isn't explicitly
|
34
|
+
given.
|
35
|
+
|
36
|
+
This plugin will first check the directory of the view being rendered,
|
37
|
+
then check the shared directories in the order given.
|
38
|
+
|
39
|
+
== REQUIREMENTS:
|
40
|
+
|
41
|
+
You'll need a recent-ish version of Rails. You'll also need hoe and
|
42
|
+
newgem, but you get them automatically when you install easypartials.
|
43
|
+
|
44
|
+
== INSTALL:
|
45
|
+
|
46
|
+
sudo gem install easy_partials
|
4
47
|
|
5
48
|
== Note on Patches/Pull Requests
|
6
49
|
|
@@ -11,7 +54,3 @@ Description goes here.
|
|
11
54
|
* Commit, do not mess with rakefile, version, or history.
|
12
55
|
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
56
|
* Send me a pull request. Bonus points for topic branches.
|
14
|
-
|
15
|
-
== Copyright
|
16
|
-
|
17
|
-
Copyright (c) 2010 Samer Abukhait. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -6,7 +6,7 @@ begin
|
|
6
6
|
Jeweler::Tasks.new do |gem|
|
7
7
|
gem.name = "easy_partials"
|
8
8
|
gem.summary = %Q{Easy Partials}
|
9
|
-
gem.description = %Q{
|
9
|
+
gem.description = %Q{An easier way to call partials}
|
10
10
|
gem.email = "abukhait@gmail.com"
|
11
11
|
gem.homepage = "http://github.com/on-site/Easy-Partials"
|
12
12
|
gem.authors = ["Samer Abukhait"]
|
data/TODO
ADDED
File without changes
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{easy_partials}
|
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 = ["Samer Abukhait"]
|
12
|
+
s.date = %q{2010-07-16}
|
13
|
+
s.description = %q{An easier way to call partials}
|
14
|
+
s.email = %q{abukhait@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc",
|
18
|
+
"TODO"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".document",
|
22
|
+
".gitignore",
|
23
|
+
"LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"TODO",
|
27
|
+
"VERSION",
|
28
|
+
"easy_partials.gemspec",
|
29
|
+
"lib/easy_partials.rb",
|
30
|
+
"spec/easy_partials_spec.rb",
|
31
|
+
"spec/spec.opts",
|
32
|
+
"spec/spec_helper.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/on-site/Easy-Partials}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubyforge_project = %q{easy_partials}
|
38
|
+
s.rubygems_version = %q{1.3.7}
|
39
|
+
s.summary = %q{Easy Partials}
|
40
|
+
s.test_files = [
|
41
|
+
"spec/easy_partials_spec.rb",
|
42
|
+
"spec/spec_helper.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
53
|
+
end
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
data/lib/easy_partials.rb
CHANGED
@@ -0,0 +1,163 @@
|
|
1
|
+
class Object
|
2
|
+
# Obtain the metaclass of this object instance.
|
3
|
+
def metaclass
|
4
|
+
class << self
|
5
|
+
self
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
# Define an instance method using a symbol for the method name and a
|
10
|
+
# block for the method contents. This makes it so a closure can be
|
11
|
+
# used to define a singleton method.
|
12
|
+
def meta_def(name, &block)
|
13
|
+
metaclass.send :define_method, name, &block
|
14
|
+
end
|
15
|
+
|
16
|
+
# Define an instance method using a block that will accept a block
|
17
|
+
# as the first parameter, and the rest of the arguments as the
|
18
|
+
# second. This requires the block to have the first parameter act
|
19
|
+
# as the block.
|
20
|
+
#
|
21
|
+
# For example:
|
22
|
+
# o.meta_def_with_block(:say_hi) { |block, name|
|
23
|
+
# block.call
|
24
|
+
# puts "hello #{name}!"
|
25
|
+
# }
|
26
|
+
#
|
27
|
+
# Will create a method that can be invoked as such:
|
28
|
+
# o.say_hi("Mike") { puts "Saying hi:" }
|
29
|
+
#
|
30
|
+
# Implicitly, 2 methods are created... __real__say_hi, which is
|
31
|
+
# created using the given block, and say_hi, which will accept the
|
32
|
+
# arguments and block and pass them to __real__say_hi in reverse
|
33
|
+
# order.
|
34
|
+
def meta_def_with_block(name, &block)
|
35
|
+
meta_def "__real__#{name}".to_sym, &block
|
36
|
+
metaclass.instance_eval "
|
37
|
+
def #{name}(*args, &block)
|
38
|
+
__real__#{name} block, *args
|
39
|
+
end
|
40
|
+
"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module EasyPartials
|
45
|
+
def self.shared_directories
|
46
|
+
@ep_shared_directories || ["shared"]
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.shared_directories=(arglist)
|
50
|
+
@ep_shared_directories = arglist
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
module ApplicationHelper
|
55
|
+
|
56
|
+
METHOD_REGEXP = /^_/
|
57
|
+
|
58
|
+
alias_method :original_respond_to?, :respond_to?
|
59
|
+
|
60
|
+
def respond_to?(method_name, inc_priv = false)
|
61
|
+
return true if method_name =~ METHOD_REGEXP
|
62
|
+
|
63
|
+
original_respond_to?(method_name, inc_priv)
|
64
|
+
end
|
65
|
+
|
66
|
+
alias_method :original_method_missing, :method_missing
|
67
|
+
|
68
|
+
def method_missing(method_name, *args, &block)
|
69
|
+
method_str = method_name.to_s
|
70
|
+
|
71
|
+
if method_str.sub! METHOD_REGEXP, ''
|
72
|
+
locations = [method_str]
|
73
|
+
locations.push *additional_partials(method_str)
|
74
|
+
new_method = partial_method locations, *args, &block
|
75
|
+
meta_def_with_block method_name, &new_method
|
76
|
+
else
|
77
|
+
original_method_missing method_name, *args, &block
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def additional_partials(partial_name)
|
82
|
+
@additional_partials ||= EasyPartials.shared_directories
|
83
|
+
@additional_partials.map { |location| "#{location}/#{partial_name}" }
|
84
|
+
end
|
85
|
+
|
86
|
+
# Utility method to create and invoke a Proc which will concat the
|
87
|
+
# partial given the possible locations. The Proc is then returned
|
88
|
+
# so it can be added as a new method for caching purposes (otherwise
|
89
|
+
# method_missing will have to be invoked each time the partial is
|
90
|
+
# invoked). The locations parameter is modified in the process.
|
91
|
+
# This is used by method_missing.
|
92
|
+
def partial_method(locations, *args, &block)
|
93
|
+
raise "No possible locations!" if locations.empty?
|
94
|
+
partial_name = locations.delete_at 0
|
95
|
+
new_method = lambda do |block, *args|
|
96
|
+
if params[:format] == "pdf"
|
97
|
+
invoke_partial partial_name, *args, &block
|
98
|
+
else
|
99
|
+
concat_partial partial_name, *args, &block
|
100
|
+
end
|
101
|
+
end
|
102
|
+
begin
|
103
|
+
new_method.call block, *args
|
104
|
+
rescue ActionView::MissingTemplate
|
105
|
+
if locations.empty?
|
106
|
+
raise
|
107
|
+
else
|
108
|
+
new_method = partial_method locations, *args, &block
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
new_method
|
113
|
+
end
|
114
|
+
|
115
|
+
def invoke_partial(partial, *args, &block)
|
116
|
+
locals = {}
|
117
|
+
|
118
|
+
if args.length == 1 && args[0].is_a?(Hash)
|
119
|
+
locals.merge! args[0]
|
120
|
+
else
|
121
|
+
locals.merge! :args => args
|
122
|
+
end
|
123
|
+
|
124
|
+
locals.merge! :body => capture(&block) if block
|
125
|
+
locals[:body] = nil unless locals[:body]
|
126
|
+
|
127
|
+
if locals.has_key? :collection
|
128
|
+
return "" if locals[:collection].blank?
|
129
|
+
render :partial => partial.to_s, :collection => locals[:collection],
|
130
|
+
:locals => locals.except(:collection)
|
131
|
+
else
|
132
|
+
render :partial => partial.to_s, :locals => locals
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
# Used to create nice templated "tags" while keeping the html out of
|
138
|
+
# our helpers. Additionally, this can be invoked implicitly by
|
139
|
+
# invoking the partial as a method with "_" prepended to the name.
|
140
|
+
#
|
141
|
+
# Invoking the method:
|
142
|
+
#
|
143
|
+
# <% concat_partial "my_partial", { :var => "value" } do %>
|
144
|
+
# <strong>Contents stored as a "body" local</strong>
|
145
|
+
# <% end %>
|
146
|
+
#
|
147
|
+
# Or invoking implicitly:
|
148
|
+
#
|
149
|
+
# <% _my_partial :var => "value" do %>
|
150
|
+
# <strong>Contents stored as a "body" local</strong>
|
151
|
+
# <% end %>
|
152
|
+
#
|
153
|
+
# Note that with the implicit partials the partial will first be
|
154
|
+
# searched for locally within the current view directory, and then
|
155
|
+
# additional directories defined by the controller level
|
156
|
+
# 'additional_partials' method, and finally within the views/shared
|
157
|
+
# directory.
|
158
|
+
def concat_partial(partial, *args, &block)
|
159
|
+
rendered = invoke_partial partial, *args, &block
|
160
|
+
concat rendered
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_partials
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 0
|
9
8
|
- 1
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Samer Abukhait
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-07-
|
18
|
+
date: 2010-07-16 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -34,7 +34,7 @@ dependencies:
|
|
34
34
|
version: 1.2.9
|
35
35
|
type: :development
|
36
36
|
version_requirements: *id001
|
37
|
-
description:
|
37
|
+
description: An easier way to call partials
|
38
38
|
email: abukhait@gmail.com
|
39
39
|
executables: []
|
40
40
|
|
@@ -43,13 +43,16 @@ extensions: []
|
|
43
43
|
extra_rdoc_files:
|
44
44
|
- LICENSE
|
45
45
|
- README.rdoc
|
46
|
+
- TODO
|
46
47
|
files:
|
47
48
|
- .document
|
48
49
|
- .gitignore
|
49
50
|
- LICENSE
|
50
51
|
- README.rdoc
|
51
52
|
- Rakefile
|
53
|
+
- TODO
|
52
54
|
- VERSION
|
55
|
+
- easy_partials.gemspec
|
53
56
|
- lib/easy_partials.rb
|
54
57
|
- spec/easy_partials_spec.rb
|
55
58
|
- spec/spec.opts
|