with_template 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eb9f1d098920eda664b0e578e87877e8484b34a0
4
+ data.tar.gz: 8877bee4586e2c50aac7aca15b325a782641a225
5
+ SHA512:
6
+ metadata.gz: 85f256b61861f7712929b1b168abfd9946ec4d3a1823d93431fd4864fe4f6d0678d159cb312ed60ce55fd8fce124ebef79672e31d642694a7b2b6f64ffac537d
7
+ data.tar.gz: a2d8e4cd17dd6e334c50090f9bffb9ecfd9bc084479a44640839c5dd67b84e931be094de718d632f3ec9032d474f303e43a1de4e96e3690bdc4ed74a42c9e97a
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ # rcov generated
2
+ coverage
3
+ coverage.data
4
+
5
+ # rdoc generated
6
+ rdoc
7
+
8
+ # yard generated
9
+ doc
10
+ .yardoc
11
+
12
+ # bundler
13
+ .bundle
14
+
15
+ # jeweler generated
16
+ pkg
17
+ Gemfile.lock
18
+
19
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
20
+ #
21
+ # * Create a file at ~/.gitignore
22
+ # * Include files you want ignored
23
+ # * Run: git config --global core.excludesfile ~/.gitignore
24
+ #
25
+ # After doing this, these files will be ignored in all your git projects,
26
+ # saving you from having to 'pollute' every project you touch with them
27
+ #
28
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
29
+ #
30
+ # For MacOS:
31
+ #
32
+ #.DS_Store
33
+
34
+ # For TextMate
35
+ #*.tmproj
36
+ #tmtags
37
+
38
+ # For emacs:
39
+ #*~
40
+ #\#*
41
+ #.\#*
42
+
43
+ # For vim:
44
+ #*.swp
45
+
46
+ # For redcar:
47
+ #.redcar
48
+
49
+ # For rubinius:
50
+ #*.rbc
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 1.9.3
1
+ 2.2.0
data/Gemfile CHANGED
@@ -1,12 +1,3 @@
1
1
  source 'http://rubygems.org'
2
2
 
3
- gem 'blocks', '~> 2.6.3'
4
- gem 'rails', '>= 3.0.0'
5
-
6
- group :development do
7
- gem 'bundler'
8
- gem 'jeweler'
9
- gem 'simplecov'
10
- gem 'shoulda'
11
- gem 'mocha'
12
- end
3
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,143 +1,135 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ with_template (0.1.0)
5
+ blocks (~> 2.7.0)
6
+ rails (>= 3.0.0)
7
+
1
8
  GEM
2
9
  remote: http://rubygems.org/
3
10
  specs:
4
- actionmailer (4.0.1)
5
- actionpack (= 4.0.1)
6
- mail (~> 2.5.4)
7
- actionpack (4.0.1)
8
- activesupport (= 4.0.1)
9
- builder (~> 3.1.0)
10
- erubis (~> 2.7.0)
11
- rack (~> 1.5.2)
11
+ actionmailer (4.2.5)
12
+ actionpack (= 4.2.5)
13
+ actionview (= 4.2.5)
14
+ activejob (= 4.2.5)
15
+ mail (~> 2.5, >= 2.5.4)
16
+ rails-dom-testing (~> 1.0, >= 1.0.5)
17
+ actionpack (4.2.5)
18
+ actionview (= 4.2.5)
19
+ activesupport (= 4.2.5)
20
+ rack (~> 1.6)
12
21
  rack-test (~> 0.6.2)
13
- activemodel (4.0.1)
14
- activesupport (= 4.0.1)
15
- builder (~> 3.1.0)
16
- activerecord (4.0.1)
17
- activemodel (= 4.0.1)
18
- activerecord-deprecated_finders (~> 1.0.2)
19
- activesupport (= 4.0.1)
20
- arel (~> 4.0.0)
21
- activerecord-deprecated_finders (1.0.3)
22
- activesupport (4.0.1)
23
- i18n (~> 0.6, >= 0.6.4)
24
- minitest (~> 4.2)
25
- multi_json (~> 1.3)
26
- thread_safe (~> 0.1)
27
- tzinfo (~> 0.3.37)
28
- addressable (2.3.5)
29
- arel (4.0.1)
30
- atomic (1.1.14)
31
- blocks (2.6.3)
22
+ rails-dom-testing (~> 1.0, >= 1.0.5)
23
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
24
+ actionview (4.2.5)
25
+ activesupport (= 4.2.5)
26
+ builder (~> 3.1)
27
+ erubis (~> 2.7.0)
28
+ rails-dom-testing (~> 1.0, >= 1.0.5)
29
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
30
+ activejob (4.2.5)
31
+ activesupport (= 4.2.5)
32
+ globalid (>= 0.3.0)
33
+ activemodel (4.2.5)
34
+ activesupport (= 4.2.5)
35
+ builder (~> 3.1)
36
+ activerecord (4.2.5)
37
+ activemodel (= 4.2.5)
38
+ activesupport (= 4.2.5)
39
+ arel (~> 6.0)
40
+ activesupport (4.2.5)
41
+ i18n (~> 0.7)
42
+ json (~> 1.7, >= 1.7.7)
43
+ minitest (~> 5.1)
44
+ thread_safe (~> 0.3, >= 0.3.4)
45
+ tzinfo (~> 1.1)
46
+ arel (6.0.3)
47
+ blocks (2.7.0)
32
48
  call_with_params (~> 0.0.2)
33
49
  hashie
34
50
  rails (>= 3.0.0)
35
- builder (3.1.4)
51
+ builder (3.2.2)
36
52
  call_with_params (0.0.2)
37
53
  activesupport (>= 3.0.0)
54
+ concurrent-ruby (1.0.0)
55
+ docile (1.1.5)
38
56
  erubis (2.7.0)
39
- faraday (0.8.8)
40
- multipart-post (~> 1.2.0)
41
- git (1.2.6)
42
- github_api (0.10.1)
43
- addressable
44
- faraday (~> 0.8.1)
45
- hashie (>= 1.2)
46
- multi_json (~> 1.4)
47
- nokogiri (~> 1.5.2)
48
- oauth2
49
- hashie (2.0.5)
50
- highline (1.6.20)
51
- hike (1.2.3)
52
- httpauth (0.2.0)
53
- i18n (0.6.5)
54
- jeweler (1.8.8)
55
- builder
56
- bundler (~> 1.0)
57
- git (>= 1.2.5)
58
- github_api (= 0.10.1)
59
- highline (>= 1.6.15)
60
- nokogiri (= 1.5.10)
61
- rake
62
- rdoc
63
- json (1.8.1)
64
- jwt (0.1.8)
65
- multi_json (>= 1.5)
66
- mail (2.5.4)
67
- mime-types (~> 1.16)
68
- treetop (~> 1.4.8)
69
- metaclass (0.0.1)
70
- mime-types (1.25)
71
- minitest (4.7.5)
72
- mocha (0.14.0)
57
+ globalid (0.3.6)
58
+ activesupport (>= 4.1.0)
59
+ hashie (3.4.3)
60
+ i18n (0.7.0)
61
+ json (1.8.3)
62
+ loofah (2.0.3)
63
+ nokogiri (>= 1.5.9)
64
+ mail (2.6.3)
65
+ mime-types (>= 1.16, < 3)
66
+ metaclass (0.0.4)
67
+ mime-types (2.99)
68
+ mini_portile2 (2.0.0)
69
+ minitest (5.8.3)
70
+ mocha (1.1.0)
73
71
  metaclass (~> 0.0.1)
74
- multi_json (1.8.2)
75
- multi_xml (0.5.5)
76
- multipart-post (1.2.0)
77
- nokogiri (1.5.10)
78
- oauth2 (0.9.2)
79
- faraday (~> 0.8)
80
- httpauth (~> 0.2)
81
- jwt (~> 0.1.4)
82
- multi_json (~> 1.0)
83
- multi_xml (~> 0.5)
84
- rack (~> 1.2)
85
- polyglot (0.3.3)
86
- rack (1.5.2)
87
- rack-test (0.6.2)
72
+ nokogiri (1.6.7)
73
+ mini_portile2 (~> 2.0.0.rc2)
74
+ rack (1.6.4)
75
+ rack-test (0.6.3)
88
76
  rack (>= 1.0)
89
- rails (4.0.1)
90
- actionmailer (= 4.0.1)
91
- actionpack (= 4.0.1)
92
- activerecord (= 4.0.1)
93
- activesupport (= 4.0.1)
77
+ rails (4.2.5)
78
+ actionmailer (= 4.2.5)
79
+ actionpack (= 4.2.5)
80
+ actionview (= 4.2.5)
81
+ activejob (= 4.2.5)
82
+ activemodel (= 4.2.5)
83
+ activerecord (= 4.2.5)
84
+ activesupport (= 4.2.5)
94
85
  bundler (>= 1.3.0, < 2.0)
95
- railties (= 4.0.1)
96
- sprockets-rails (~> 2.0.0)
97
- railties (4.0.1)
98
- actionpack (= 4.0.1)
99
- activesupport (= 4.0.1)
86
+ railties (= 4.2.5)
87
+ sprockets-rails
88
+ rails-deprecated_sanitizer (1.0.3)
89
+ activesupport (>= 4.2.0.alpha)
90
+ rails-dom-testing (1.0.7)
91
+ activesupport (>= 4.2.0.beta, < 5.0)
92
+ nokogiri (~> 1.6.0)
93
+ rails-deprecated_sanitizer (>= 1.0.1)
94
+ rails-html-sanitizer (1.0.2)
95
+ loofah (~> 2.0)
96
+ railties (4.2.5)
97
+ actionpack (= 4.2.5)
98
+ activesupport (= 4.2.5)
100
99
  rake (>= 0.8.7)
101
100
  thor (>= 0.18.1, < 2.0)
102
- rake (10.1.0)
103
- rdoc (4.0.1)
104
- json (~> 1.4)
101
+ rake (10.4.2)
105
102
  shoulda (3.5.0)
106
103
  shoulda-context (~> 1.0, >= 1.0.1)
107
104
  shoulda-matchers (>= 1.4.1, < 3.0)
108
- shoulda-context (1.1.5)
109
- shoulda-matchers (2.4.0)
105
+ shoulda-context (1.2.1)
106
+ shoulda-matchers (2.8.0)
110
107
  activesupport (>= 3.0.0)
111
- simplecov (0.7.1)
112
- multi_json (~> 1.0)
113
- simplecov-html (~> 0.7.1)
114
- simplecov-html (0.7.1)
115
- sprockets (2.10.0)
116
- hike (~> 1.2)
117
- multi_json (~> 1.0)
118
- rack (~> 1.0)
119
- tilt (~> 1.1, != 1.3.0)
120
- sprockets-rails (2.0.1)
108
+ simplecov (0.11.1)
109
+ docile (~> 1.1.0)
110
+ json (~> 1.8)
111
+ simplecov-html (~> 0.10.0)
112
+ simplecov-html (0.10.0)
113
+ sprockets (3.5.2)
114
+ concurrent-ruby (~> 1.0)
115
+ rack (> 1, < 3)
116
+ sprockets-rails (2.3.3)
121
117
  actionpack (>= 3.0)
122
118
  activesupport (>= 3.0)
123
- sprockets (~> 2.8)
124
- thor (0.18.1)
125
- thread_safe (0.1.3)
126
- atomic
127
- tilt (1.4.1)
128
- treetop (1.4.15)
129
- polyglot
130
- polyglot (>= 0.3.1)
131
- tzinfo (0.3.38)
119
+ sprockets (>= 2.8, < 4.0)
120
+ thor (0.19.1)
121
+ thread_safe (0.3.5)
122
+ tzinfo (1.2.2)
123
+ thread_safe (~> 0.1)
132
124
 
133
125
  PLATFORMS
134
126
  ruby
135
127
 
136
128
  DEPENDENCIES
137
- blocks (~> 2.6.3)
138
- bundler
139
- jeweler
140
129
  mocha
141
- rails (>= 3.0.0)
142
- shoulda
130
+ shoulda (~> 3.5.0)
143
131
  simplecov
132
+ with_template!
133
+
134
+ BUNDLED WITH
135
+ 1.10.6
data/README.rdoc CHANGED
@@ -1,14 +1,163 @@
1
1
  = with_template
2
2
 
3
- Render a template (partial) and easily override any of the components of the template.
3
+ WithTemplate takes the concept of {Blocks}[https://github.com/hunterae/blocks] and adds templating to it. You specify a particular partial to render, and then maintain the ability to control how it renders from outside the template.
4
+
5
+ This allows you to very easily build reusable UI components. This is best demonstrated through examples.
4
6
 
5
7
  == Installation
6
8
 
7
9
  To install, add the following to the Gemfile:
8
10
  gem install 'with_template'
9
11
 
10
- == Copyright
12
+ == Note (12/14/15)
13
+ I am in the process of doing a full rewrite of this readme file to include better examples, and better description
14
+ of the features including new features that have not been documented yet.
15
+
16
+ == Example 1: TableFor
17
+ The {TableFor}[https://github.com/hunterae/table-for] was built by extending WithTemplate. Using a partial that defines a
18
+ series of blocks ({See here}[https://github.com/hunterae/table-for/blob/master/app/views/table_for/_table_for.html.erb]),
19
+ the user of the table builder is passed in a collection of records and specifies exactly which columns to render in the table.
20
+
21
+ The columns can correspond to fields are the records, methods to be invoked, or the user can specify exactly what should
22
+ be rendered in the column. Additionally, the headers can be overridden, any any component of the table can be replaced
23
+ should the user not like how the table renders. A simple example would be:
24
+
25
+ <%= table_for @users, :table_html => {:class => "table table-hover table-bordered"} do |table| %>
26
+ <% table.column :id, :header => false %>
27
+ <% table.column :full_name do |user| %>
28
+ <%= "#{user.first_name} #{user.last_name}" %>
29
+ <% end %>
30
+ <% table.footer do %>
31
+ <div class="pull-right">
32
+ <%= will_paginate @users %>
33
+ </div>
34
+ <% end %>
35
+ <% end %>
36
+
37
+ == Example 2: Shared Layouts
38
+ Say you wanted to wanted to create the following three layouts for your application:
39
+ 1) a single full-width column
40
+ 2) two columns, one that is a fourth of the page, and one that is three fourths of the page.
41
+ 3) three columns, each a third of the page
42
+
43
+ To accomplish this, you may find yourself copying and pasting code between layouts, such as the head tag,
44
+ any meta tags, javascript includes, stylesheet links, navigation bar, the footer, and any components shared across all pages.
45
+
46
+ Perhaps you typically take this approach, or maybe you render a partial that you pass local variables
47
+ to that are used within the partial to conditionally render output.
48
+
49
+ I’d like to propose a third solution: use with_template.
50
+
51
+ With with_template, you specify what template (really just a partial) to render and a block that
52
+ can be used to override definitions within the partial, control what gets rendered and in
53
+ what order, or the block itself can be rendered in the partial.
54
+
55
+ With the above example, we could define a single partial, something like app/layouts/_shared_layout.html.erb
56
+ (Note: If you aren't familiar with the {Blocks gem}[https://github.com/hunterae/blocks], I highly recommend checking out
57
+ the documentation first):
58
+
59
+ <!DOCTYPE html>
60
+ <html lang="en">
61
+ <head>
62
+ <!-- meta tags -->
63
+ <title>
64
+ <%= blocks.render :title do %>
65
+ My App
66
+ <% end %>
67
+ </title>
68
+ <%= csrf_meta_tags %>
69
+ <%= blocks.render :stylesheets do %>
70
+ <%= stylesheet_link_tag "application", :media => "all" %>
71
+ <% end %>
72
+ <!-- Additional <head> content -->
73
+ </head>
74
+ <body>
75
+ <%= blocks.render_with_partials :navbar %>
76
+ <div class="main">
77
+ <div class="container">
78
+ <div class="row">
79
+ <div class="col-md-12">
80
+ <%= bootstrap_flash %>
81
+ </div>
82
+ <%= captured_block %>
83
+ </div>
84
+ </div>
85
+ </div>
86
+ <%= blocks.render :footer do %>
87
+ <%= render "blocks/footer" %>
88
+ <% end %>
89
+ <%= blocks.render :javascripts do %>
90
+ <%= include_gon %>
91
+ <%= javascript_include_tag "application" %>
92
+ <% end %>
93
+ </body>
94
+ </html>
95
+
96
+ Here we've defined a template of code that can be shared amongst all layouts. Where the individual layouts differ, there
97
+ are several hooks that have been added to the template that provide convenient ways of adding code blocks or replacing
98
+ the way others render. In the above code, we call "blocks.render" and "blocks.render_with_partials" in a couple of different
99
+ places. These allow us, from outside the template, to specify code to render before any of those blocks, after any of those
100
+ blocks, or replace any of those blocks. We'll get to that shortly.
101
+
102
+ Using the above partial, we can now very easily define the 3 layouts defined above:
103
+
104
+ <!-- app/views/layout/one_column.html.erb -->
105
+ <%= with_template("layouts/shared_layout") do %>
106
+ <div class="col-md-12">
107
+ <%= yield %>
108
+ </div>
109
+ <% end %>
110
+
111
+ <!-- app/views/layout/two_columns.html.erb -->
112
+ <%= with_template("layouts/shared_layout") do %>
113
+ <%= blocks.render_with_partials :sidebar, :wrap_with => { :tag => :div, :class => "col-md-3 sidebar" } %>
114
+ <div class="col-md-9">
115
+ <%= yield %>
116
+ </div>
117
+ <% end %>
118
+
119
+ <!-- app/views/layout/three_columns.html.erb -->
120
+ <%= with_template("layouts/shared_layout") do %>
121
+ <%= blocks.render :column1, :wrap_with => { :tag => :div, :class => "col-md-4" } %>
122
+ <%= blocks.render :column2, :wrap_with => { :tag => :div, :class => "col-md-4" } %>
123
+ <%= blocks.render :column3, :wrap_with => { :tag => :div, :class => "col-md-4" } %>
124
+ <% end %>
125
+
126
+ Suppose we wanted to add a fourth layout for admins that used roughly the same code as the one column layout,
127
+ but with some additional javascript and stylesheet files included. In the true spirit of DRY, this is
128
+ exceptionally easy to do with WithTemplate:
129
+
130
+ <!-- app/views/layout/admin.html.erb -->
131
+ <% blocks.after :stylesheets do %>
132
+ <%= stylesheet_link_tag "admin", :media => "all" %>
133
+ <% end %>
134
+
135
+ <% blocks.after :javascripts do %>
136
+ <%= javascript_include_tag "admin" %>
137
+ <% end %>
138
+
139
+ <% blocks.define :title do %>
140
+ Admin
141
+ <% end %>
142
+
143
+ <%= render :template => "layouts/one_column" %>
144
+
145
+ With those lines, we added in the admin javascript files, the admin stylesheets, set the title to "Admin",
146
+ and reused the basic one column layout. What if, instead of rendering the admin stylesheets and javascripts
147
+ after the application stylesheets and javascripts, we wanted to flat out replace the application stylesheets
148
+ and javascripts. Simple enough:
149
+
150
+ <!-- app/views/layout/admin.html.erb -->
151
+ <% blocks.define :stylesheets do %>
152
+ <%= stylesheet_link_tag "admin", :media => "all" %>
153
+ <% end %>
154
+
155
+ <% blocks.define :javascripts do %>
156
+ <%= javascript_include_tag "admin" %>
157
+ <% end %>
11
158
 
12
- Copyright (c) 2013 Andrew Hunter. See LICENSE.txt for
13
- further details.
159
+ <% blocks.define :title do %>
160
+ Admin
161
+ <% end %>
14
162
 
163
+ <%= render :template => "layouts/one_column" %>
data/Rakefile CHANGED
@@ -1,29 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'rubygems'
4
- require 'bundler'
5
- begin
6
- Bundler.setup(:default, :development)
7
- rescue Bundler::BundlerError => e
8
- $stderr.puts e.message
9
- $stderr.puts "Run `bundle install` to install missing gems"
10
- exit e.status_code
11
- end
12
- require 'rake'
13
-
14
- require 'jeweler'
15
- Jeweler::Tasks.new do |gem|
16
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
- gem.name = "with_template"
18
- gem.homepage = "http://github.com/hunterae/with_template"
19
- gem.license = "MIT"
20
- gem.summary = %Q{Render a template (partial) and easily override any of the components of the template}
21
- gem.description = %Q{Render a template (partial) and easily override any of the components of the template}
22
- gem.email = "hunterae@gmail.com"
23
- gem.authors = ["Andrew Hunter"]
24
- # dependencies defined in Gemfile
25
- end
26
- Jeweler::RubygemsDotOrgTasks.new
4
+ require 'bundler/gem_tasks'
27
5
 
28
6
  require 'rake/testtask'
29
7
  Rake::TestTask.new(:test) do |test|
data/lib/with_template.rb CHANGED
@@ -6,4 +6,4 @@ module WithTemplate
6
6
  autoload :ViewAdditions, "with_template/view_additions"
7
7
  end
8
8
 
9
- ActionView::Base.send :include, WithTemplate::ViewAdditions::ClassMethods
9
+ ActionView::Base.send :include, WithTemplate::ViewAdditions
@@ -3,8 +3,8 @@ module WithTemplate
3
3
  # Array of Blocks::Container objects, storing the order of blocks as they were queued
4
4
  attr_accessor :queued_blocks
5
5
 
6
- # A Hash of queued_blocks arrays; a new array is started when method_missing is invoked
7
- attr_accessor :block_groups
6
+ # Options that were passed into this instance of WithTemplate for use when merging options with a parent instance
7
+ attr_accessor :init_options
8
8
 
9
9
  # Render a partial, treating it as a template, and any code in the block argument will impact how the template renders
10
10
  # <%= WithTemplate::Base.new(self).render_template("shared/wizard") do |blocks| %>
@@ -41,10 +41,12 @@ module WithTemplate
41
41
  # The partial to render as a template
42
42
  # [+block+]
43
43
  # An optional block with code that affects how the template renders
44
- def render_template(partial, &block)
44
+ def render_template(partial, variable=nil, &block)
45
45
  render_options = global_options.clone
46
- render_options[render_options[:variable]] = self
47
46
  render_options[:captured_block] = view.capture(self, &block) if block_given?
47
+ render_options[:options] = render_options
48
+ variable ||= render_options.delete(:variable) || :template
49
+ render_options[variable] = self
48
50
 
49
51
  view.render partial, render_options
50
52
  end
@@ -95,30 +97,8 @@ module WithTemplate
95
97
 
96
98
  def initialize(view, options={})
97
99
  self.queued_blocks = []
98
- self.block_groups = {}
99
- super(view, options.reverse_merge(:variable => :template))
100
- end
101
-
102
- # If a method is missing, we'll assume the user is starting a new block group by that missing method name
103
- def method_missing(m, *args, &block)
104
- options = args.extract_options!
105
-
106
- # If the specified block group has already been defined, it is simply returned here for iteration.
107
- # It will consist of all the blocks used in this block group that have yet to be rendered,
108
- # as the call for their use occurred before the template was rendered (where their definitions likely occurred)
109
- return self.block_groups[m] unless self.block_groups[m].nil?
110
-
111
- # Allows for nested block groups, store the current block positions array and start a new one
112
- original_queued_blocks = self.queued_blocks
113
- self.queued_blocks = []
114
- self.block_groups[m] = self.queued_blocks
115
-
116
- # Capture the contents of the block group (this will only capture block definitions and block renders; it will ignore anything else)
117
- view.capture(global_options.merge(options), &block) if block_given?
118
-
119
- # restore the original block positions array
120
- self.queued_blocks = original_queued_blocks
121
- nil
100
+ self.init_options = options
101
+ super(view, options)
122
102
  end
123
103
  end
124
104
  end
@@ -0,0 +1,3 @@
1
+ module WithTemplate
2
+ VERSION = "0.1.0"
3
+ end
@@ -1,9 +1,31 @@
1
1
  module WithTemplate
2
2
  module ViewAdditions
3
- module ClassMethods
4
- def with_template(template, &block)
5
- WithTemplate::Base.new(self).render_template(template, &block)
3
+ def with_template(template, options={}, &block)
4
+ options = options.with_indifferent_access
5
+ template_variable = options.delete(:variable) || :template
6
+ WithTemplate::Base.new(self, options).render_template(template, template_variable, &block)
7
+ end
8
+
9
+ def with_global_template(template, options={}, &block)
10
+ options = options.with_indifferent_access
11
+ variable = options.delete(:variable) || :global_template
12
+ if @global_template.present?
13
+ @global_template.init_options.reverse_merge!(options)
14
+ @global_template.global_options.merge!(@global_template.init_options)
15
+ else
16
+ @global_template = WithTemplate::Base.new(self, options)
17
+ end
18
+ @global_template.render_template(template, variable, &block)
19
+ end
20
+
21
+ def global_template
22
+ if @global_template.blank?
23
+ @global_template = WithTemplate::Base.new(self)
24
+ @global_template.blocks = blocks.blocks
25
+ @global_template.skipped_blocks = blocks.skipped_blocks
26
+ @global_template.anonymous_block_number = blocks.anonymous_block_number
6
27
  end
28
+ @global_template
7
29
  end
8
30
  end
9
- end
31
+ end
data/test/base_test.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class WithTemplate::BaseTest < Test::Unit::TestCase
3
+ class WithTemplate::BaseTest < MiniTest::Unit::TestCase
4
4
  def setup
5
5
  @view = ActionView::Base.new
6
6
  @builder = WithTemplate::Base.new(@view)
@@ -28,11 +28,17 @@ class WithTemplate::BaseTest < Test::Unit::TestCase
28
28
  @builder.render_template("some_template")
29
29
  end
30
30
 
31
- should "add allow the variable in the partial to be overridden" do
32
- builder = WithTemplate::Base.new(@view, :variable => :new_template_variable)
31
+ should "allow the variable in the partial to be overridden in the options hash" do
32
+ builder = WithTemplate::Base.new(@view, variable: :new_template_variable)
33
33
  @view.expects(:render).with { |template, options| options[:new_template_variable] == builder }
34
34
  builder.render_template("some_template")
35
35
  end
36
+
37
+ should "allow the variable in the partial to be overridden as a parameter" do
38
+ builder = WithTemplate::Base.new(@view)
39
+ @view.expects(:render).with { |template, options| options[:new_template_variable] == builder }
40
+ builder.render_template("some_template", :new_template_variable)
41
+ end
36
42
  end
37
43
 
38
44
  context "#queue" do
@@ -89,30 +95,4 @@ class WithTemplate::BaseTest < Test::Unit::TestCase
89
95
  assert_equal block, container.block
90
96
  end
91
97
  end
92
-
93
- context "#method_missing" do
94
- should "start a new block group if a method is missing" do
95
- @builder.some_method
96
- queued_blocks = @builder.block_groups[:some_method]
97
- assert_equal [], queued_blocks
98
- end
99
-
100
- should "add items to a queue when a new block group is started" do
101
- @builder.some_method do
102
- @builder.queue :myblock1
103
- @builder.queue :myblock2
104
- end
105
- @builder.some_method2 do
106
- @builder.queue :myblock3
107
- end
108
- queued_blocks = @builder.block_groups[:some_method]
109
- assert_equal 2, queued_blocks.length
110
- assert_equal :myblock1, queued_blocks.first.name
111
- assert_equal :myblock2, queued_blocks.second.name
112
- queued_blocks = @builder.block_groups[:some_method2]
113
- assert_equal 1, queued_blocks.length
114
- assert_equal :myblock3, queued_blocks.first.name
115
- assert_equal [], @builder.queued_blocks
116
- end
117
- end
118
98
  end
File without changes
data/test/helper.rb CHANGED
@@ -1,20 +1,6 @@
1
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
-
12
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'minitest/autorun'
14
3
  require 'shoulda'
15
- require 'mocha'
16
- require 'active_support/core_ext'
4
+ require 'mocha/setup'
17
5
  require 'blocks'
18
6
  require 'with_template'
19
-
20
- ActiveSupport::Deprecation.silenced = true
@@ -0,0 +1,124 @@
1
+ require 'helper'
2
+
3
+ class WithTemplate::ViewAdditionsTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ @view = ActionView::Base.new(File.dirname(__FILE__) + '/fixtures')
6
+ @block = Proc.new {}
7
+ end
8
+
9
+ context "#with_template" do
10
+ should "instantiate a new WithTemplate::Base instance and render the template with it" do
11
+ options={ a: 1, b: 2}
12
+ instance = mock
13
+ instance.expects(:render_template).with("my_template", :template)
14
+ WithTemplate::Base.expects(:new).with(@view, options.with_indifferent_access).returns(instance)
15
+ @view.with_template("my_template", options, &@block)
16
+ end
17
+
18
+ should "allow and extract a variable parameter to be passed in the options hash" do
19
+ options = { variable: :my_variable, a: 1, b: 2 }
20
+ instance = mock
21
+ instance.expects(:render_template).with("my_template", :my_variable)
22
+ WithTemplate::Base.expects(:new).with(@view, options.with_indifferent_access.except(:variable)).returns(instance)
23
+ @view.with_template("my_template", options, &@block)
24
+ end
25
+
26
+ should "require only the first parameter to be specified" do
27
+ @view.with_template("my_template")
28
+ assert_raises ArgumentError do
29
+ @view.with_template
30
+ end
31
+ end
32
+ end
33
+
34
+ context "#with_global_template" do
35
+ context "when @global_template is not yet defined in the view" do
36
+ setup do
37
+ assert @view.instance_variable_get(:@global_template).nil?
38
+ end
39
+
40
+ should "instantiate a new WithTemplate::Base instance and render the template with it" do
41
+ options = {a: 1, b: 2}
42
+ instance = mock
43
+ instance.expects(:render_template).with("my_template", :global_template)
44
+ WithTemplate::Base.expects(:new).with(@view, options.with_indifferent_access).returns(instance)
45
+ @view.with_global_template("my_template", options, &@block)
46
+ end
47
+
48
+ should "allow and extract a variable parameter to be passed in the options hash" do
49
+ options = { variable: :my_variable, a: 1, b: 2 }
50
+ instance = mock
51
+ instance.expects(:render_template).with("my_template", :my_variable)
52
+ WithTemplate::Base.expects(:new).with(@view, options.with_indifferent_access.except(:variable)).returns(instance)
53
+ @view.with_global_template("my_template", options, &@block)
54
+ end
55
+
56
+ should "require only the first parameter to be specified" do
57
+ @view.with_global_template("my_template")
58
+ assert_raises ArgumentError do
59
+ @view.with_global_template
60
+ end
61
+ end
62
+ end
63
+
64
+ context "when @global_template is already set on the view" do
65
+ setup do
66
+ @view.global_template
67
+ @global_template = @view.instance_variable_get(:@global_template)
68
+ assert @global_template.present?
69
+ WithTemplate::Base.expects(:new).never
70
+ end
71
+
72
+ should "use the existing WithTemplate::Base instance and render the template with it" do
73
+ options={}
74
+ WithTemplate::Base.any_instance.expects(:render_template).with("my_template", :global_template)
75
+ @view.with_global_template("my_template", options, &@block)
76
+ end
77
+
78
+ should "allow and extract a variable parameter to be passed in the options hash" do
79
+ WithTemplate::Base.any_instance.expects(:render_template).with("my_template", :something)
80
+
81
+ @view.with_global_template("my_template", variable: :something)
82
+ assert @view.global_template.global_options[:variable].blank?
83
+ end
84
+
85
+ should "give precedence to the init options set in previous calls to with_global_template" do
86
+ options_1 = { a: 1, b: 2, d: 3 }
87
+ options_2 = { a: 4, b: 5, c: 6 }
88
+ options_3 = { a: 7, b: 8, c: 9, d: 10, e: 11}
89
+ merged = options_3.merge(options_2.merge(options_1)).with_indifferent_access
90
+ @view.with_global_template("my_template", options_1) do
91
+ @view.with_global_template("my_template", options_2) do
92
+ @view.with_global_template("my_template", options_3)
93
+ end
94
+ end
95
+ assert_equal @global_template.init_options, merged
96
+ assert_equal @global_template.global_options, @view.blocks.global_options.merge(merged)
97
+ end
98
+ end
99
+ end
100
+
101
+ context "#global_template" do
102
+ should "return a memoized global_template" do
103
+ assert_same @view.global_template, @view.global_template
104
+ end
105
+
106
+ should "instantiate the global_template from the global blocks instance" do
107
+ @view.blocks.skip(:some_block)
108
+ @view.blocks.define(:hello) do
109
+ "Goodbye"
110
+ end
111
+ @view.blocks.define(nil) do
112
+ "Anonymous block"
113
+ end
114
+ gt = @view.global_template
115
+ assert gt.skipped_blocks.include?(:some_block)
116
+ assert_equal 1, gt.anonymous_block_number
117
+ assert gt.blocks.include?(:block_1)
118
+ assert gt.blocks.include?(:hello)
119
+ assert_equal "Goodbye", gt.render(:hello)
120
+ assert_equal "Anonymous block", gt.render(:block_1)
121
+ assert_equal @view.blocks.global_options, gt.global_options
122
+ end
123
+ end
124
+ end
@@ -1,73 +1,28 @@
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
1
 
6
- Gem::Specification.new do |s|
7
- s.name = "with_template"
8
- s.version = "0.0.6"
2
+ # coding: utf-8
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'with_template/version'
9
6
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Andrew Hunter"]
12
- s.date = "2013-11-04"
13
- s.description = "Render a template (partial) and easily override any of the components of the template"
14
- s.email = "hunterae@gmail.com"
15
- s.extra_rdoc_files = [
16
- "LICENSE.txt",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".document",
21
- ".ruby-gemset",
22
- ".ruby-version",
23
- "Gemfile",
24
- "Gemfile.lock",
25
- "LICENSE.txt",
26
- "README.rdoc",
27
- "Rakefile",
28
- "VERSION",
29
- "lib/with_template.rb",
30
- "lib/with_template/base.rb",
31
- "lib/with_template/view_additions.rb",
32
- "rails/init.rb",
33
- "test/base_test.rb",
34
- "test/helper.rb",
35
- "with_template.gemspec"
36
- ]
37
- s.homepage = "http://github.com/hunterae/with_template"
38
- s.licenses = ["MIT"]
39
- s.require_paths = ["lib"]
40
- s.rubygems_version = "1.8.25"
41
- s.summary = "Render a template (partial) and easily override any of the components of the template"
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "with_template"
9
+ spec.version = WithTemplate::VERSION
10
+ spec.authors = ["Andrew Hunter"]
11
+ spec.email = ["hunterae@gmail.com"]
12
+ spec.summary = %q{Render a template (partial) and easily override any of the components of the template}
13
+ spec.description = %q{Render a template (partial) and easily override any of the components of the template}
14
+ spec.homepage = "http://github.com/hunterae/with_template"
15
+ spec.license = "MIT"
42
16
 
43
- if s.respond_to? :specification_version then
44
- s.specification_version = 3
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
45
21
 
46
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
- s.add_runtime_dependency(%q<blocks>, ["~> 2.6.3"])
48
- s.add_runtime_dependency(%q<rails>, [">= 3.0.0"])
49
- s.add_development_dependency(%q<bundler>, [">= 0"])
50
- s.add_development_dependency(%q<jeweler>, [">= 0"])
51
- s.add_development_dependency(%q<simplecov>, [">= 0"])
52
- s.add_development_dependency(%q<shoulda>, [">= 0"])
53
- s.add_development_dependency(%q<mocha>, [">= 0"])
54
- else
55
- s.add_dependency(%q<blocks>, ["~> 2.6.3"])
56
- s.add_dependency(%q<rails>, [">= 3.0.0"])
57
- s.add_dependency(%q<bundler>, [">= 0"])
58
- s.add_dependency(%q<jeweler>, [">= 0"])
59
- s.add_dependency(%q<simplecov>, [">= 0"])
60
- s.add_dependency(%q<shoulda>, [">= 0"])
61
- s.add_dependency(%q<mocha>, [">= 0"])
62
- end
63
- else
64
- s.add_dependency(%q<blocks>, ["~> 2.6.3"])
65
- s.add_dependency(%q<rails>, [">= 3.0.0"])
66
- s.add_dependency(%q<bundler>, [">= 0"])
67
- s.add_dependency(%q<jeweler>, [">= 0"])
68
- s.add_dependency(%q<simplecov>, [">= 0"])
69
- s.add_dependency(%q<shoulda>, [">= 0"])
70
- s.add_dependency(%q<mocha>, [">= 0"])
71
- end
72
- end
22
+ spec.add_dependency "rails", ">= 3.0.0"
23
+ spec.add_dependency "blocks", "~> 2.7.0"
73
24
 
25
+ spec.add_development_dependency "simplecov"
26
+ spec.add_development_dependency "shoulda", "~> 3.5.0"
27
+ spec.add_development_dependency "mocha"
28
+ end
metadata CHANGED
@@ -1,180 +1,138 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: with_template
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Andrew Hunter
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-11-04 00:00:00.000000000 Z
11
+ date: 2015-12-14 00:00:00.000000000 Z
13
12
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: blocks
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: 2.6.3
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- version: 2.6.3
30
13
  - !ruby/object:Gem::Dependency
31
14
  name: rails
32
15
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
16
  requirements:
35
- - - ! '>='
17
+ - - ">="
36
18
  - !ruby/object:Gem::Version
37
19
  version: 3.0.0
38
20
  type: :runtime
39
21
  prerelease: false
40
22
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
23
  requirements:
43
- - - ! '>='
24
+ - - ">="
44
25
  - !ruby/object:Gem::Version
45
26
  version: 3.0.0
46
27
  - !ruby/object:Gem::Dependency
47
- name: bundler
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: '0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- - !ruby/object:Gem::Dependency
63
- name: jeweler
28
+ name: blocks
64
29
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
30
  requirements:
67
- - - ! '>='
31
+ - - "~>"
68
32
  - !ruby/object:Gem::Version
69
- version: '0'
70
- type: :development
33
+ version: 2.7.0
34
+ type: :runtime
71
35
  prerelease: false
72
36
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
37
  requirements:
75
- - - ! '>='
38
+ - - "~>"
76
39
  - !ruby/object:Gem::Version
77
- version: '0'
40
+ version: 2.7.0
78
41
  - !ruby/object:Gem::Dependency
79
42
  name: simplecov
80
43
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
44
  requirements:
83
- - - ! '>='
45
+ - - ">="
84
46
  - !ruby/object:Gem::Version
85
47
  version: '0'
86
48
  type: :development
87
49
  prerelease: false
88
50
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
51
  requirements:
91
- - - ! '>='
52
+ - - ">="
92
53
  - !ruby/object:Gem::Version
93
54
  version: '0'
94
55
  - !ruby/object:Gem::Dependency
95
56
  name: shoulda
96
57
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
58
  requirements:
99
- - - ! '>='
59
+ - - "~>"
100
60
  - !ruby/object:Gem::Version
101
- version: '0'
61
+ version: 3.5.0
102
62
  type: :development
103
63
  prerelease: false
104
64
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
65
  requirements:
107
- - - ! '>='
66
+ - - "~>"
108
67
  - !ruby/object:Gem::Version
109
- version: '0'
68
+ version: 3.5.0
110
69
  - !ruby/object:Gem::Dependency
111
70
  name: mocha
112
71
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
72
  requirements:
115
- - - ! '>='
73
+ - - ">="
116
74
  - !ruby/object:Gem::Version
117
75
  version: '0'
118
76
  type: :development
119
77
  prerelease: false
120
78
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
79
  requirements:
123
- - - ! '>='
80
+ - - ">="
124
81
  - !ruby/object:Gem::Version
125
82
  version: '0'
126
83
  description: Render a template (partial) and easily override any of the components
127
84
  of the template
128
- email: hunterae@gmail.com
85
+ email:
86
+ - hunterae@gmail.com
129
87
  executables: []
130
88
  extensions: []
131
- extra_rdoc_files:
132
- - LICENSE.txt
133
- - README.rdoc
89
+ extra_rdoc_files: []
134
90
  files:
135
- - .document
136
- - .ruby-gemset
137
- - .ruby-version
91
+ - ".document"
92
+ - ".gitignore"
93
+ - ".ruby-version"
138
94
  - Gemfile
139
95
  - Gemfile.lock
140
96
  - LICENSE.txt
141
97
  - README.rdoc
142
98
  - Rakefile
143
- - VERSION
144
99
  - lib/with_template.rb
145
100
  - lib/with_template/base.rb
101
+ - lib/with_template/version.rb
146
102
  - lib/with_template/view_additions.rb
147
103
  - rails/init.rb
148
104
  - test/base_test.rb
105
+ - test/fixtures/_my_template.html.erb
149
106
  - test/helper.rb
107
+ - test/view_additions_test.rb
150
108
  - with_template.gemspec
151
109
  homepage: http://github.com/hunterae/with_template
152
110
  licenses:
153
111
  - MIT
112
+ metadata: {}
154
113
  post_install_message:
155
114
  rdoc_options: []
156
115
  require_paths:
157
116
  - lib
158
117
  required_ruby_version: !ruby/object:Gem::Requirement
159
- none: false
160
118
  requirements:
161
- - - ! '>='
119
+ - - ">="
162
120
  - !ruby/object:Gem::Version
163
121
  version: '0'
164
- segments:
165
- - 0
166
- hash: -2512220499031150591
167
122
  required_rubygems_version: !ruby/object:Gem::Requirement
168
- none: false
169
123
  requirements:
170
- - - ! '>='
124
+ - - ">="
171
125
  - !ruby/object:Gem::Version
172
126
  version: '0'
173
127
  requirements: []
174
128
  rubyforge_project:
175
- rubygems_version: 1.8.25
129
+ rubygems_version: 2.4.6
176
130
  signing_key:
177
- specification_version: 3
131
+ specification_version: 4
178
132
  summary: Render a template (partial) and easily override any of the components of
179
133
  the template
180
- test_files: []
134
+ test_files:
135
+ - test/base_test.rb
136
+ - test/fixtures/_my_template.html.erb
137
+ - test/helper.rb
138
+ - test/view_additions_test.rb
data/.ruby-gemset DELETED
@@ -1 +0,0 @@
1
- use_template
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.0.6