ampere 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +15 -0
- data/Gemfile +2 -2
- data/Gemfile.lock +36 -26
- data/README.md +25 -11
- data/Rakefile +10 -0
- data/VERSION +1 -1
- data/ampere.gemspec +83 -9
- data/example/.gitignore +5 -0
- data/example/.rspec +1 -0
- data/example/Gemfile +21 -0
- data/example/Gemfile.lock +143 -0
- data/example/README +261 -0
- data/example/Rakefile +7 -0
- data/example/app/assets/images/rails.png +0 -0
- data/example/app/assets/javascripts/application.js +9 -0
- data/example/app/assets/javascripts/posts.js.coffee +3 -0
- data/example/app/assets/stylesheets/application.css +7 -0
- data/example/app/assets/stylesheets/posts.css.scss +3 -0
- data/example/app/assets/stylesheets/scaffolds.css.scss +56 -0
- data/example/app/controllers/application_controller.rb +3 -0
- data/example/app/controllers/posts_controller.rb +83 -0
- data/example/app/helpers/application_helper.rb +2 -0
- data/example/app/helpers/posts_helper.rb +2 -0
- data/example/app/mailers/.gitkeep +0 -0
- data/example/app/models/.gitkeep +0 -0
- data/example/app/models/post.rb +7 -0
- data/example/app/views/layouts/application.html.erb +14 -0
- data/example/app/views/posts/_form.html.erb +25 -0
- data/example/app/views/posts/edit.html.erb +6 -0
- data/example/app/views/posts/index.html.erb +25 -0
- data/example/app/views/posts/new.html.erb +5 -0
- data/example/app/views/posts/show.html.erb +15 -0
- data/example/config/ampere.yml +11 -0
- data/example/config/application.rb +54 -0
- data/example/config/boot.rb +6 -0
- data/example/config/environment.rb +5 -0
- data/example/config/environments/development.rb +30 -0
- data/example/config/environments/production.rb +60 -0
- data/example/config/environments/test.rb +39 -0
- data/example/config/initializers/backtrace_silencers.rb +7 -0
- data/example/config/initializers/inflections.rb +10 -0
- data/example/config/initializers/mime_types.rb +5 -0
- data/example/config/initializers/secret_token.rb +7 -0
- data/example/config/initializers/session_store.rb +8 -0
- data/example/config/initializers/wrap_parameters.rb +10 -0
- data/example/config/locales/en.yml +5 -0
- data/example/config/routes.rb +60 -0
- data/example/config.ru +4 -0
- data/example/db/seeds.rb +7 -0
- data/example/lib/assets/.gitkeep +0 -0
- data/example/lib/tasks/.gitkeep +0 -0
- data/example/log/.gitkeep +0 -0
- data/example/public/404.html +26 -0
- data/example/public/422.html +26 -0
- data/example/public/500.html +26 -0
- data/example/public/favicon.ico +0 -0
- data/example/public/index.html +241 -0
- data/example/public/robots.txt +5 -0
- data/example/script/rails +6 -0
- data/example/spec/controllers/posts_controller_spec.rb +164 -0
- data/example/spec/helpers/posts_helper_spec.rb +15 -0
- data/example/spec/models/post_spec.rb +5 -0
- data/example/spec/requests/posts_spec.rb +11 -0
- data/example/spec/routing/posts_routing_spec.rb +35 -0
- data/example/spec/spec_helper.rb +34 -0
- data/example/spec/views/posts/edit.html.erb_spec.rb +20 -0
- data/example/spec/views/posts/index.html.erb_spec.rb +23 -0
- data/example/spec/views/posts/new.html.erb_spec.rb +20 -0
- data/example/spec/views/posts/show.html.erb_spec.rb +17 -0
- data/example/vendor/assets/stylesheets/.gitkeep +0 -0
- data/example/vendor/plugins/.gitkeep +0 -0
- data/lib/ampere/collection.rb +6 -0
- data/lib/ampere/keys.rb +37 -0
- data/lib/ampere/model.rb +117 -58
- data/lib/ampere/timestamps.rb +23 -0
- data/lib/ampere.rb +17 -1
- data/lib/rails/generators/ampere/config/config_generator.rb +15 -0
- data/lib/rails/generators/ampere/config/templates/ampere.yml +11 -0
- data/lib/rails/generators/ampere/model/model_generator.rb +27 -0
- data/lib/rails/generators/ampere/model/templates/model.rb.tt +10 -0
- data/lib/rails/railtie.rb +49 -0
- data/lib/rails/tasks/ampere.rake +7 -0
- data/spec/models/model_spec.rb +12 -2
- data/spec/models/timestamps_spec.rb +45 -0
- data/spec/module/collections_spec.rb +5 -0
- data/spec/spec_helper.rb +1 -0
- metadata +153 -29
@@ -0,0 +1,164 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# This spec was generated by rspec-rails when you ran the scaffold generator.
|
4
|
+
# It demonstrates how one might use RSpec to specify the controller code that
|
5
|
+
# was generated by Rails when you ran the scaffold generator.
|
6
|
+
#
|
7
|
+
# It assumes that the implementation code is generated by the rails scaffold
|
8
|
+
# generator. If you are using any extension libraries to generate different
|
9
|
+
# controller code, this generated spec may or may not pass.
|
10
|
+
#
|
11
|
+
# It only uses APIs available in rails and/or rspec-rails. There are a number
|
12
|
+
# of tools you can use to make these specs even more expressive, but we're
|
13
|
+
# sticking to rails and rspec-rails APIs to keep things simple and stable.
|
14
|
+
#
|
15
|
+
# Compared to earlier versions of this generator, there is very limited use of
|
16
|
+
# stubs and message expectations in this spec. Stubs are only used when there
|
17
|
+
# is no simpler way to get a handle on the object needed for the example.
|
18
|
+
# Message expectations are only used when there is no simpler way to specify
|
19
|
+
# that an instance is receiving a specific message.
|
20
|
+
|
21
|
+
describe PostsController do
|
22
|
+
|
23
|
+
# This should return the minimal set of attributes required to create a valid
|
24
|
+
# Post. As you add validations to Post, be sure to
|
25
|
+
# update the return value of this method accordingly.
|
26
|
+
def valid_attributes
|
27
|
+
{}
|
28
|
+
end
|
29
|
+
|
30
|
+
# This should return the minimal set of values that should be in the session
|
31
|
+
# in order to pass any filters (e.g. authentication) defined in
|
32
|
+
# PostsController. Be sure to keep this updated too.
|
33
|
+
def valid_session
|
34
|
+
{}
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "GET index" do
|
38
|
+
it "assigns all posts as @posts" do
|
39
|
+
post = Post.create! valid_attributes
|
40
|
+
get :index, {}, valid_session
|
41
|
+
assigns(:posts).to_a.should eq([post])
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "GET show" do
|
46
|
+
it "assigns the requested post as @post" do
|
47
|
+
post = Post.create! valid_attributes
|
48
|
+
get :show, {:id => post.to_param}, valid_session
|
49
|
+
assigns(:post).should eq(post)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "GET new" do
|
54
|
+
it "assigns a new post as @post" do
|
55
|
+
get :new, {}, valid_session
|
56
|
+
assigns(:post).should be_a_new(Post)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "GET edit" do
|
61
|
+
it "assigns the requested post as @post" do
|
62
|
+
post = Post.create! valid_attributes
|
63
|
+
get :edit, {:id => post.to_param}, valid_session
|
64
|
+
assigns(:post).should eq(post)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "POST create" do
|
69
|
+
describe "with valid params" do
|
70
|
+
it "creates a new Post" do
|
71
|
+
expect {
|
72
|
+
post :create, {:post => valid_attributes}, valid_session
|
73
|
+
}.to change(Post, :count).by(1)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "assigns a newly created post as @post" do
|
77
|
+
post :create, {:post => valid_attributes}, valid_session
|
78
|
+
assigns(:post).should be_a(Post)
|
79
|
+
assigns(:post).should be_persisted
|
80
|
+
end
|
81
|
+
|
82
|
+
it "redirects to the created post" do
|
83
|
+
post :create, {:post => valid_attributes}, valid_session
|
84
|
+
response.should redirect_to(Post.last)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "with invalid params" do
|
89
|
+
it "assigns a newly created but unsaved post as @post" do
|
90
|
+
# Trigger the behavior that occurs when invalid params are submitted
|
91
|
+
Post.any_instance.stub(:save).and_return(false)
|
92
|
+
post :create, {:post => {}}, valid_session
|
93
|
+
assigns(:post).should be_a_new(Post)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "re-renders the 'new' template" do
|
97
|
+
# Trigger the behavior that occurs when invalid params are submitted
|
98
|
+
Post.any_instance.stub(:save).and_return(false)
|
99
|
+
post :create, {:post => {}}, valid_session
|
100
|
+
response.should render_template("new")
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "PUT update" do
|
106
|
+
describe "with valid params" do
|
107
|
+
it "updates the requested post" do
|
108
|
+
post = Post.create! valid_attributes
|
109
|
+
# Assuming there are no other posts in the database, this
|
110
|
+
# specifies that the Post created on the previous line
|
111
|
+
# receives the :update_attributes message with whatever params are
|
112
|
+
# submitted in the request.
|
113
|
+
Post.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
|
114
|
+
put :update, {:id => post.to_param, :post => {'these' => 'params'}}, valid_session
|
115
|
+
end
|
116
|
+
|
117
|
+
it "assigns the requested post as @post" do
|
118
|
+
post = Post.create! valid_attributes
|
119
|
+
put :update, {:id => post.to_param, :post => valid_attributes}, valid_session
|
120
|
+
assigns(:post).should eq(post)
|
121
|
+
end
|
122
|
+
|
123
|
+
it "redirects to the post" do
|
124
|
+
post = Post.create! valid_attributes
|
125
|
+
put :update, {:id => post.to_param, :post => valid_attributes}, valid_session
|
126
|
+
response.should redirect_to(post)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "with invalid params" do
|
131
|
+
it "assigns the post as @post" do
|
132
|
+
post = Post.create! valid_attributes
|
133
|
+
# Trigger the behavior that occurs when invalid params are submitted
|
134
|
+
Post.any_instance.stub(:save).and_return(false)
|
135
|
+
put :update, {:id => post.to_param, :post => {}}, valid_session
|
136
|
+
assigns(:post).should eq(post)
|
137
|
+
end
|
138
|
+
|
139
|
+
it "re-renders the 'edit' template" do
|
140
|
+
post = Post.create! valid_attributes
|
141
|
+
# Trigger the behavior that occurs when invalid params are submitted
|
142
|
+
Post.any_instance.stub(:save).and_return(false)
|
143
|
+
put :update, {:id => post.to_param, :post => {}}, valid_session
|
144
|
+
response.should render_template("edit")
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
describe "DELETE destroy" do
|
150
|
+
it "destroys the requested post" do
|
151
|
+
post = Post.create! valid_attributes
|
152
|
+
expect {
|
153
|
+
delete :destroy, {:id => post.to_param}, valid_session
|
154
|
+
}.to change(Post, :count).by(-1)
|
155
|
+
end
|
156
|
+
|
157
|
+
it "redirects to the posts list" do
|
158
|
+
post = Post.create! valid_attributes
|
159
|
+
delete :destroy, {:id => post.to_param}, valid_session
|
160
|
+
response.should redirect_to(posts_url)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# Specs in this file have access to a helper object that includes
|
4
|
+
# the PostsHelper. For example:
|
5
|
+
#
|
6
|
+
# describe PostsHelper do
|
7
|
+
# describe "string concat" do
|
8
|
+
# it "concats two strings with spaces" do
|
9
|
+
# helper.concat_strings("this","that").should == "this that"
|
10
|
+
# end
|
11
|
+
# end
|
12
|
+
# end
|
13
|
+
describe PostsHelper do
|
14
|
+
pending "add some examples to (or delete) #{__FILE__}"
|
15
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Posts" do
|
4
|
+
describe "GET /posts" do
|
5
|
+
it "works! (now write some real specs)" do
|
6
|
+
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
|
7
|
+
get posts_path
|
8
|
+
response.status.should be(200)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe PostsController do
|
4
|
+
describe "routing" do
|
5
|
+
|
6
|
+
it "routes to #index" do
|
7
|
+
get("/posts").should route_to("posts#index")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "routes to #new" do
|
11
|
+
get("/posts/new").should route_to("posts#new")
|
12
|
+
end
|
13
|
+
|
14
|
+
it "routes to #show" do
|
15
|
+
get("/posts/1").should route_to("posts#show", :id => "1")
|
16
|
+
end
|
17
|
+
|
18
|
+
it "routes to #edit" do
|
19
|
+
get("/posts/1/edit").should route_to("posts#edit", :id => "1")
|
20
|
+
end
|
21
|
+
|
22
|
+
it "routes to #create" do
|
23
|
+
post("/posts").should route_to("posts#create")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "routes to #update" do
|
27
|
+
put("/posts/1").should route_to("posts#update", :id => "1")
|
28
|
+
end
|
29
|
+
|
30
|
+
it "routes to #destroy" do
|
31
|
+
delete("/posts/1").should route_to("posts#destroy", :id => "1")
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# This file is copied to spec/ when you run 'rails generate rspec:install'
|
2
|
+
ENV["RAILS_ENV"] ||= 'test'
|
3
|
+
require File.expand_path("../../config/environment", __FILE__)
|
4
|
+
require 'rspec/rails'
|
5
|
+
require 'rspec/autorun'
|
6
|
+
|
7
|
+
# Requires supporting ruby files with custom matchers and macros, etc,
|
8
|
+
# in spec/support/ and its subdirectories.
|
9
|
+
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
# ## Mock Framework
|
13
|
+
#
|
14
|
+
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
|
15
|
+
#
|
16
|
+
# config.mock_with :mocha
|
17
|
+
# config.mock_with :flexmock
|
18
|
+
# config.mock_with :rr
|
19
|
+
|
20
|
+
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
|
21
|
+
config.fixture_path = "#{::Rails.root}/spec/fixtures"
|
22
|
+
|
23
|
+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
|
24
|
+
# examples within a transaction, remove the following line or assign false
|
25
|
+
# instead of true.
|
26
|
+
config.use_transactional_fixtures = true
|
27
|
+
|
28
|
+
# If true, the base class of anonymous controllers will be inferred
|
29
|
+
# automatically. This will be the default behavior in future versions of
|
30
|
+
# rspec-rails.
|
31
|
+
config.infer_base_class_for_anonymous_controllers = false
|
32
|
+
end
|
33
|
+
|
34
|
+
Ampere.flush
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "posts/edit" do
|
4
|
+
before(:each) do
|
5
|
+
@post = assign(:post, stub_model(Post,
|
6
|
+
:title => "MyString",
|
7
|
+
:body => "MyString"
|
8
|
+
))
|
9
|
+
end
|
10
|
+
|
11
|
+
it "renders the edit post form" do
|
12
|
+
render
|
13
|
+
|
14
|
+
# Run the generator again with the --webrat flag if you want to use webrat matchers
|
15
|
+
assert_select "form", :action => posts_path(@post), :method => "post" do
|
16
|
+
assert_select "input#post_title", :name => "post[title]"
|
17
|
+
assert_select "input#post_body", :name => "post[body]"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "posts/index" do
|
4
|
+
before(:each) do
|
5
|
+
assign(:posts, [
|
6
|
+
stub_model(Post,
|
7
|
+
:title => "Title",
|
8
|
+
:body => "Body"
|
9
|
+
),
|
10
|
+
stub_model(Post,
|
11
|
+
:title => "Title",
|
12
|
+
:body => "Body"
|
13
|
+
)
|
14
|
+
])
|
15
|
+
end
|
16
|
+
|
17
|
+
it "renders a list of posts" do
|
18
|
+
render
|
19
|
+
# Run the generator again with the --webrat flag if you want to use webrat matchers
|
20
|
+
assert_select "tr>td", :text => "Title".to_s, :count => 2
|
21
|
+
assert_select "tr>td", :text => "Body".to_s, :count => 2
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "posts/new" do
|
4
|
+
before(:each) do
|
5
|
+
assign(:post, stub_model(Post,
|
6
|
+
:title => "MyString",
|
7
|
+
:body => "MyString"
|
8
|
+
).as_new_record)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "renders new post form" do
|
12
|
+
render
|
13
|
+
|
14
|
+
# Run the generator again with the --webrat flag if you want to use webrat matchers
|
15
|
+
assert_select "form", :action => posts_path, :method => "post" do
|
16
|
+
assert_select "input#post_title", :name => "post[title]"
|
17
|
+
assert_select "input#post_body", :name => "post[body]"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "posts/show" do
|
4
|
+
before(:each) do
|
5
|
+
@post = assign(:post, stub_model(Post,
|
6
|
+
:title => "Title",
|
7
|
+
:body => "Body"
|
8
|
+
))
|
9
|
+
end
|
10
|
+
|
11
|
+
it "renders attributes in <p>" do
|
12
|
+
render
|
13
|
+
# Run the generator again with the --webrat flag if you want to use webrat matchers
|
14
|
+
rendered.should match(/Title/)
|
15
|
+
rendered.should match(/Body/)
|
16
|
+
end
|
17
|
+
end
|
File without changes
|
File without changes
|
data/lib/ampere/collection.rb
CHANGED
data/lib/ampere/keys.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# These are just utlity functions used by Ampere internally to generate Redis
|
2
|
+
# keys for various Ampere functions, for DRY excellence.
|
3
|
+
module Ampere #:nodoc:
|
4
|
+
module Keys #:nodoc:
|
5
|
+
# These methods get mixed in to class and instance
|
6
|
+
def self.included(base)
|
7
|
+
# base.extend(ClassMethods)
|
8
|
+
base.extend(self)
|
9
|
+
end
|
10
|
+
|
11
|
+
def key_for_find(parent_model, id)
|
12
|
+
unless id =~ /\./
|
13
|
+
id = "#{parent_model.to_s.downcase}.#{id}"
|
14
|
+
end
|
15
|
+
id
|
16
|
+
end
|
17
|
+
|
18
|
+
def key_for_has_many(parent_model, id, field)
|
19
|
+
[parent_model, id, 'has_many', field].flatten.join('.')
|
20
|
+
end
|
21
|
+
|
22
|
+
def key_for_index(field)
|
23
|
+
['ampere', 'index', model_name.downcase, field].flatten.join('.')
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def model_name
|
29
|
+
if self.class == Class then
|
30
|
+
to_s
|
31
|
+
else
|
32
|
+
self.class.to_s
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|