stachio 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,6 +1,7 @@
1
1
  .bundle/
2
2
  log/*.log
3
3
  *.gem
4
+ Gemfile.lock
4
5
  pkg/
5
6
  spec/dummy/db/*.sqlite3
6
7
  spec/dummy/log/*.log
data/.simplecov CHANGED
@@ -9,13 +9,4 @@ unless ENV['COVERAGE'] == 'false'
9
9
  end
10
10
 
11
11
  SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
12
-
13
- SimpleCov.start('rails') do
14
- add_filter "vendor/ruby/"
15
- add_filter "spec/dummy"
16
-
17
- add_group "Models", "app/models"
18
- add_group "Views", "app/views"
19
- add_group "Controllers", "app/controllers"
20
- end
21
12
  end
@@ -1,4 +1,7 @@
1
1
  module Stachio
2
2
  class ApplicationController < ActionController::Base
3
+ def readonly
4
+ !!(Stachio::Engine.config.readonly)
5
+ end
3
6
  end
4
7
  end
@@ -2,6 +2,10 @@ require_dependency "stachio/application_controller"
2
2
 
3
3
  module Stachio
4
4
  class TemplatesController < ApplicationController
5
+
6
+ before_filter :permission
7
+ skip_before_filter :permission, :only => [:index, :show]
8
+
5
9
  # GET /templates
6
10
  # GET /templates.json
7
11
  def index
@@ -83,5 +87,13 @@ module Stachio
83
87
  format.json { head :no_content }
84
88
  end
85
89
  end
90
+
91
+ private
92
+
93
+ def permission
94
+ return true unless readonly
95
+ flash[:notice] = "Templates are readonly"
96
+ redirect_to :action => :index
97
+ end
86
98
  end
87
99
  end
@@ -1,4 +1,7 @@
1
1
  module Stachio
2
2
  module ApplicationHelper
3
+ def readonly
4
+ !!(Stachio::Engine.config.readonly)
5
+ end
3
6
  end
4
7
  end
@@ -1,4 +1,7 @@
1
1
  module Stachio
2
2
  module TemplatesHelper
3
+ def readonly
4
+ !!(Stachio::Engine.config.readonly)
5
+ end
3
6
  end
4
7
  end
@@ -1,21 +1,31 @@
1
- require 'stachio/proxy'
2
-
3
1
  module Stachio
2
+ Mustache.raise_on_context_miss = true
3
+
4
4
  class Template < ActiveRecord::Base
5
5
  lookup_by :template_name
6
6
 
7
7
  attr_accessible :template_name, :content
8
8
  validates_presence_of :template_name, :content
9
9
 
10
- attr_accessor :proxied
10
+ attr_accessor :presents, :rendered
11
+
12
+ def reset
13
+ self.rendered = nil
14
+ end
11
15
 
12
- def proxy
13
- @proxy ||= Stachio::Proxy.new(proxied) unless proxied.nil?
16
+ def render(options={})
17
+ options = options.with_indifferent_access
18
+ reset if !!(options[:force] or options[:reset])
19
+ self.rendered ||= Mustache.render(content, presents) unless presents.nil?
14
20
  end
21
+ alias_method :assemble, :render
22
+ alias_method :composite, :render
23
+ alias_method :compose, :render
15
24
 
16
- def compose
17
- proxy.template = content
18
- proxy.render
25
+ def present(values=nil, options={})
26
+ return render if values.nil?
27
+ self.presents = values
28
+ render options.merge(:force => true)
19
29
  end
20
30
  end
21
31
  end
@@ -8,7 +8,13 @@
8
8
  </head>
9
9
  <body>
10
10
 
11
- <%= yield %>
11
+ <% if flash.any? %>
12
+ <% flash.each do |key, value| %>
13
+ <%= content_tag(:div, value, :class => "flash #{key}") %>
14
+ <% end %>
15
+ <% end %>
16
+
17
+ <%= yield %>
12
18
 
13
19
  </body>
14
20
  </html>
@@ -14,12 +14,16 @@
14
14
  <td><%= template.template_name %></td>
15
15
  <td><%= template.content %></td>
16
16
  <td><%= link_to 'Show', template %></td>
17
- <td><%= link_to 'Edit', edit_template_path(template) %></td>
18
- <td><%= link_to 'Destroy', template, method: :delete, data: { confirm: 'Are you sure?' } %></td>
17
+ <% unless readonly %>
18
+ <td><%= link_to 'Edit', edit_template_path(template) %></td>
19
+ <td><%= link_to 'Destroy', template, method: :delete, data: { confirm: 'Are you sure?' } %></td>
20
+ <% end %>
19
21
  </tr>
20
22
  <% end %>
21
23
  </table>
22
24
 
23
25
  <br />
24
26
 
25
- <%= link_to 'New Template', new_template_path %>
27
+ <% unless readonly %>
28
+ <%= link_to 'New Template', new_template_path %>
29
+ <% end %>
@@ -11,5 +11,7 @@
11
11
  </p>
12
12
 
13
13
 
14
- <%= link_to 'Edit', edit_template_path(@template) %> |
14
+ <% unless readonly %>
15
+ <%= link_to 'Edit', edit_template_path(@template) %> |
16
+ <% end %>
15
17
  <%= link_to 'Back', templates_path %>
@@ -1,4 +1,3 @@
1
1
  Stachio::Engine.routes.draw do
2
- resources :templates
3
- root :to => 'templates#index'
2
+ resources :templates, :path => '/'
4
3
  end
@@ -9,6 +9,8 @@ module Stachio
9
9
  require "stache" ## use mustache/handlebars for views
10
10
  Stache.use :mustache
11
11
 
12
+ config.readonly = false unless defined?(Rails) && Rails.env.production?
13
+
12
14
  config.generators do |g|
13
15
  g.test_framework :rspec
14
16
  g.integration_tool :rspec
@@ -1,3 +1,3 @@
1
1
  module Stachio
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -20,6 +20,9 @@ require 'spec_helper'
20
20
 
21
21
  module Stachio
22
22
  describe TemplatesController do
23
+ # 'render_views' will render any erb /haml/whatever as part of the tests, thus
24
+ # providing some (minimal) assurance that nothing stupid was done
25
+ render_views
23
26
 
24
27
  # This should return the minimal set of attributes required to create a valid
25
28
  # Template. As you add validations to Template, be sure to
@@ -41,6 +44,13 @@ module Stachio
41
44
  get :index, {}, valid_session
42
45
  assigns(:templates).should eq([template])
43
46
  end
47
+
48
+ it "does not require permission" do
49
+ Stachio::Engine.config.stub(:readonly).and_return(true)
50
+ template = Template.create! valid_attributes
51
+ get :index, {}, valid_session
52
+ response.should_not redirect_to(templates_path)
53
+ end
44
54
  end
45
55
 
46
56
  describe "GET show" do
@@ -49,9 +59,22 @@ module Stachio
49
59
  get :show, {:id => template.to_param}, valid_session
50
60
  assigns(:template).should eq(template)
51
61
  end
62
+
63
+ it "does not require permission" do
64
+ Stachio::Engine.config.stub(:readonly).and_return(true)
65
+ template = Template.create! valid_attributes
66
+ get :show, {:id => template.to_param}, valid_session
67
+ response.should_not redirect_to(templates_path)
68
+ end
52
69
  end
53
70
 
54
71
  describe "GET new" do
72
+ it "requires permission" do
73
+ Stachio::Engine.config.stub(:readonly).and_return(true)
74
+ get :new, {}, valid_session
75
+ response.should redirect_to(templates_path)
76
+ end
77
+
55
78
  it "assigns a new template as @template" do
56
79
  get :new, {}, valid_session
57
80
  assigns(:template).should be_a_new(Template)
@@ -59,6 +82,13 @@ module Stachio
59
82
  end
60
83
 
61
84
  describe "GET edit" do
85
+ it "requires permission" do
86
+ Stachio::Engine.config.stub(:readonly).and_return(true)
87
+ template = Template.create! valid_attributes
88
+ get :edit, {:id => template.to_param}, valid_session
89
+ response.should redirect_to(templates_path)
90
+ end
91
+
62
92
  it "assigns the requested template as @template" do
63
93
  template = Template.create! valid_attributes
64
94
  get :edit, {:id => template.to_param}, valid_session
@@ -68,6 +98,14 @@ module Stachio
68
98
 
69
99
  describe "POST create" do
70
100
  describe "with valid params" do
101
+ it "requires permission" do
102
+ Stachio::Engine.config.stub(:readonly).and_return(true)
103
+ expect {
104
+ post :create, {:template => valid_attributes}, valid_session
105
+ }.to_not change(Template, :count).by(1)
106
+ response.should redirect_to(templates_path)
107
+ end
108
+
71
109
  it "creates a new Template" do
72
110
  expect {
73
111
  post :create, {:template => valid_attributes}, valid_session
@@ -105,6 +143,14 @@ module Stachio
105
143
 
106
144
  describe "PUT update" do
107
145
  describe "with valid params" do
146
+ it "requires permission" do
147
+ template = Template.create! valid_attributes
148
+ Stachio::Engine.config.stub(:readonly).and_return(true)
149
+ Template.any_instance.should_not_receive(:update_attributes).with({ "template_name" => "MyString" })
150
+ put :update, {:id => template.to_param, :template => { "template_name" => "MyString" }}, valid_session
151
+ response.should redirect_to(templates_path)
152
+ end
153
+
108
154
  it "updates the requested template" do
109
155
  template = Template.create! valid_attributes
110
156
  # Assuming there are no other templates in the database, this
@@ -148,6 +194,17 @@ module Stachio
148
194
  end
149
195
 
150
196
  describe "DELETE destroy" do
197
+ it "requires permission" do
198
+ template = Template.create! valid_attributes
199
+ Stachio::Engine.config.stub(:readonly).and_return(true)
200
+
201
+ expect {
202
+ delete :destroy, {:id => template.to_param}, valid_session
203
+ }.to_not change(Template, :count).by(-1)
204
+
205
+ response.should redirect_to(templates_path)
206
+ end
207
+
151
208
  it "destroys the requested template" do
152
209
  template = Template.create! valid_attributes
153
210
  expect {
@@ -1,20 +1,16 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module Stachio
4
- class MockProxyObject
5
- def teapot
6
- "TEAPOT"
4
+ describe Template do
5
+ let(:presents) do
6
+ Class.new { def self.teapot; 'TEAPOT'; end }
7
7
  end
8
- end
9
8
 
10
- describe Template do
11
9
  let(:template) do
12
- attrs = { :template_name => "my silly teapot",
13
- :content => "I am a {{teapot}}",
14
- }
15
- template = Template.new(attrs)
16
- template.proxied = MockProxyObject.new
17
- template
10
+ tmpl = Template.new :template_name => "mrs.potts",
11
+ :content => "I am a {{teapot}}"
12
+ tmpl.presents = presents
13
+ tmpl
18
14
  end
19
15
 
20
16
  it "stores templates in the database" do
@@ -39,16 +35,56 @@ module Stachio
39
35
 
40
36
  it "fetches templates from the database using the template name" do
41
37
  template.save!
42
- template.should == Template['my silly teapot']
43
- end
44
-
45
- it "composes messages using the content of the template" do
46
- template.compose.should == "I am a TEAPOT"
38
+ template.should == Template['mrs.potts']
47
39
  end
48
40
 
49
41
  it "reports an error when it can't compose the message" do
50
42
  template.content = "I am not a valid {{horseradish}}"
51
- expect { template.compose }.to raise_error(Mustache::ContextMiss)
43
+ expect { template.render }.to raise_error(Mustache::ContextMiss)
52
44
  end
45
+
46
+ describe '#render' do
47
+ it "renders using the content of the template" do
48
+ template.presents.should be_a_kind_of(Class)
49
+ template.render.should == "I am a TEAPOT"
50
+ end
51
+
52
+ it "can render values from a hash" do
53
+ template.presents = {:teapot => 'TEAPOT' }
54
+ template.render.should == "I am a TEAPOT"
55
+ end
56
+
57
+ it "renders nothing when presenting a nil" do
58
+ template.presents = nil
59
+ template.render.should == nil
60
+ end
61
+
62
+ it "memoizes rendered text" do
63
+ template.render.should == "I am a TEAPOT"
64
+ template.rendered.should == "I am a TEAPOT"
65
+
66
+ template.presents = {:teapot => 'HORSERADISH'}
67
+
68
+ template.render.should == "I am a TEAPOT"
69
+ template.rendered.should == "I am a TEAPOT"
70
+ end
71
+
72
+ it "can be reset when memoized" do
73
+ template.render.should == "I am a TEAPOT"
74
+ template.presents = {:teapot => 'HORSERADISH'}
75
+
76
+ template.render.should == "I am a TEAPOT"
77
+ template.render(:force => true).should == "I am a HORSERADISH"
78
+ end
79
+ end
80
+
81
+ describe '#present' do
82
+ it "sets attribute 'presents' & renders all at once" do
83
+ template.render.should == "I am a TEAPOT"
84
+ template.present(:teapot => 'TURNIP').should == "I am a TURNIP"
85
+ template.presents.should == {:teapot => 'TURNIP'}
86
+ end
87
+ end
88
+
53
89
  end
54
90
  end
@@ -5,31 +5,31 @@ module Stachio
5
5
  describe "routing" do
6
6
 
7
7
  it "routes to #index" do
8
- get("/templates").should route_to("stachio/templates#index")
8
+ get("/").should route_to("stachio/templates#index")
9
9
  end
10
10
 
11
11
  it "routes to #new" do
12
- get("/templates/new").should route_to("stachio/templates#new")
12
+ get("/new").should route_to("stachio/templates#new")
13
13
  end
14
14
 
15
15
  it "routes to #show" do
16
- get("/templates/1").should route_to("stachio/templates#show", :id => "1")
16
+ get("/1").should route_to("stachio/templates#show", :id => "1")
17
17
  end
18
18
 
19
19
  it "routes to #edit" do
20
- get("/templates/1/edit").should route_to("stachio/templates#edit", :id => "1")
20
+ get("/1/edit").should route_to("stachio/templates#edit", :id => "1")
21
21
  end
22
22
 
23
23
  it "routes to #create" do
24
- post("/templates").should route_to("stachio/templates#create")
24
+ post("/").should route_to("stachio/templates#create")
25
25
  end
26
26
 
27
27
  it "routes to #update" do
28
- put("/templates/1").should route_to("stachio/templates#update", :id => "1")
28
+ put("/1").should route_to("stachio/templates#update", :id => "1")
29
29
  end
30
30
 
31
31
  it "routes to #destroy" do
32
- delete("/templates/1").should route_to("stachio/templates#destroy", :id => "1")
32
+ delete("/1").should route_to("stachio/templates#destroy", :id => "1")
33
33
  end
34
34
 
35
35
  end
@@ -1,3 +1,13 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.start do
4
+ add_filter "vendor"
5
+ add_filter "spec"
6
+
7
+ add_group "Models", "app/models"
8
+ add_group "Controllers", "app/controllers"
9
+ end
10
+
1
11
  # This file is copied to spec/ when you run 'rails generate rspec:install'
2
12
  ENV["RAILS_ENV"] ||= 'test'
3
13
 
@@ -6,7 +16,6 @@ require File.expand_path("../dummy/config/environment", __FILE__)
6
16
 
7
17
  require 'rspec/rails'
8
18
  require 'rspec/autorun'
9
- require 'simplecov'
10
19
  require 'pry'
11
20
 
12
21
  # Requires supporting ruby files with custom matchers and macros, etc,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stachio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-05-13 00:00:00.000000000 Z
13
+ date: 2013-05-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -234,7 +234,6 @@ files:
234
234
  - .ruby-version
235
235
  - .simplecov
236
236
  - Gemfile
237
- - Gemfile.lock
238
237
  - MIT-LICENSE
239
238
  - README.md
240
239
  - Rakefile
@@ -259,7 +258,6 @@ files:
259
258
  - db/migrate/20130509172418_create_stachio_templates.rb
260
259
  - lib/stachio.rb
261
260
  - lib/stachio/engine.rb
262
- - lib/stachio/proxy.rb
263
261
  - lib/stachio/version.rb
264
262
  - lib/tasks/stachio_tasks.rake
265
263
  - script/rails
@@ -1,152 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- stachio (0.0.1)
5
- handlebars
6
- jquery-rails
7
- lookup_by
8
- mustache
9
- rails (~> 3.2.13)
10
- stache
11
-
12
- GEM
13
- remote: http://rubygems.org/
14
- specs:
15
- actionmailer (3.2.13)
16
- actionpack (= 3.2.13)
17
- mail (~> 2.5.3)
18
- actionpack (3.2.13)
19
- activemodel (= 3.2.13)
20
- activesupport (= 3.2.13)
21
- builder (~> 3.0.0)
22
- erubis (~> 2.7.0)
23
- journey (~> 1.0.4)
24
- rack (~> 1.4.5)
25
- rack-cache (~> 1.2)
26
- rack-test (~> 0.6.1)
27
- sprockets (~> 2.2.1)
28
- activemodel (3.2.13)
29
- activesupport (= 3.2.13)
30
- builder (~> 3.0.0)
31
- activerecord (3.2.13)
32
- activemodel (= 3.2.13)
33
- activesupport (= 3.2.13)
34
- arel (~> 3.0.2)
35
- tzinfo (~> 0.3.29)
36
- activeresource (3.2.13)
37
- activemodel (= 3.2.13)
38
- activesupport (= 3.2.13)
39
- activesupport (3.2.13)
40
- i18n (= 0.6.1)
41
- multi_json (~> 1.0)
42
- arel (3.0.2)
43
- builder (3.0.4)
44
- coderay (1.0.9)
45
- commonjs (0.2.6)
46
- diff-lcs (1.2.4)
47
- erubis (2.7.0)
48
- handlebars (0.4.0)
49
- commonjs (~> 0.2.3)
50
- therubyracer (~> 0.11.1)
51
- hike (1.2.2)
52
- i18n (0.6.1)
53
- journey (1.0.4)
54
- jquery-rails (2.2.1)
55
- railties (>= 3.0, < 5.0)
56
- thor (>= 0.14, < 2.0)
57
- json (1.7.7)
58
- libv8 (3.11.8.17)
59
- lookup_by (0.1.0)
60
- rails (>= 3.0.0)
61
- mail (2.5.3)
62
- i18n (>= 0.4.0)
63
- mime-types (~> 1.16)
64
- treetop (~> 1.4.8)
65
- method_source (0.8.1)
66
- mime-types (1.23)
67
- multi_json (1.7.3)
68
- mustache (0.99.4)
69
- polyglot (0.3.3)
70
- pry (0.9.12.1)
71
- coderay (~> 1.0.5)
72
- method_source (~> 0.8)
73
- slop (~> 3.4)
74
- rack (1.4.5)
75
- rack-cache (1.2)
76
- rack (>= 0.4)
77
- rack-ssl (1.3.3)
78
- rack
79
- rack-test (0.6.2)
80
- rack (>= 1.0)
81
- rails (3.2.13)
82
- actionmailer (= 3.2.13)
83
- actionpack (= 3.2.13)
84
- activerecord (= 3.2.13)
85
- activeresource (= 3.2.13)
86
- activesupport (= 3.2.13)
87
- bundler (~> 1.0)
88
- railties (= 3.2.13)
89
- railties (3.2.13)
90
- actionpack (= 3.2.13)
91
- activesupport (= 3.2.13)
92
- rack-ssl (~> 1.3.2)
93
- rake (>= 0.8.7)
94
- rdoc (~> 3.4)
95
- thor (>= 0.14.6, < 2.0)
96
- rake (10.0.4)
97
- rdoc (3.12.2)
98
- json (~> 1.4)
99
- ref (1.0.4)
100
- rspec (2.13.0)
101
- rspec-core (~> 2.13.0)
102
- rspec-expectations (~> 2.13.0)
103
- rspec-mocks (~> 2.13.0)
104
- rspec-core (2.13.1)
105
- rspec-expectations (2.13.0)
106
- diff-lcs (>= 1.1.3, < 2.0)
107
- rspec-fire (1.1.3)
108
- rspec (~> 2.11)
109
- rspec-mocks (2.13.1)
110
- rspec-rails (2.13.1)
111
- actionpack (>= 3.0)
112
- activesupport (>= 3.0)
113
- railties (>= 3.0)
114
- rspec-core (~> 2.13.0)
115
- rspec-expectations (~> 2.13.0)
116
- rspec-mocks (~> 2.13.0)
117
- simplecov (0.7.1)
118
- multi_json (~> 1.0)
119
- simplecov-html (~> 0.7.1)
120
- simplecov-html (0.7.1)
121
- simplecov-rcov (0.2.3)
122
- simplecov (>= 0.4.1)
123
- slop (3.4.4)
124
- sprockets (2.2.2)
125
- hike (~> 1.2)
126
- multi_json (~> 1.0)
127
- rack (~> 1.0)
128
- tilt (~> 1.1, != 1.3.0)
129
- sqlite3 (1.3.7)
130
- stache (1.0.1)
131
- therubyracer (0.11.4)
132
- libv8 (~> 3.11.8.12)
133
- ref
134
- thor (0.18.1)
135
- tilt (1.4.1)
136
- treetop (1.4.12)
137
- polyglot
138
- polyglot (>= 0.3.1)
139
- tzinfo (0.3.37)
140
-
141
- PLATFORMS
142
- ruby
143
-
144
- DEPENDENCIES
145
- pry
146
- rspec
147
- rspec-fire
148
- rspec-rails
149
- simplecov
150
- simplecov-rcov
151
- sqlite3
152
- stachio!
@@ -1,20 +0,0 @@
1
- module Stachio
2
- class Proxy < Mustache
3
- self.raise_on_context_miss = true
4
- attr_accessor :proxied
5
-
6
- def initialize(proxied)
7
- self.proxied = proxied
8
- end
9
-
10
- def respond_to?(method, include_privates=false)
11
- return true if proxied.respond_to? method
12
- super
13
- end
14
-
15
- def method_missing(method, *args, &block)
16
- super unless proxied.respond_to?(method)
17
- proxied.send method, *args, &block
18
- end
19
- end
20
- end