cruddy 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +21 -0
- data/lib/cruddy.rb +4 -0
- data/lib/cruddy/controller.rb +127 -0
- data/lib/cruddy/spec_support.rb +2 -0
- data/lib/cruddy/spec_support/create_examples.rb +47 -0
- data/lib/cruddy/spec_support/cruddy_examples.rb +18 -0
- data/lib/cruddy/spec_support/destroy_examples.rb +31 -0
- data/lib/cruddy/spec_support/edit_examples.rb +24 -0
- data/lib/cruddy/spec_support/index_examples.rb +24 -0
- data/lib/cruddy/spec_support/new_examples.rb +19 -0
- data/lib/cruddy/spec_support/show_examples.rb +24 -0
- data/lib/cruddy/spec_support/update_examples.rb +43 -0
- data/lib/cruddy/version.rb +3 -0
- data/lib/tasks/cruddy_tasks.rake +4 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 90532271103c41782049c9aacbf400c7bb0f105f
|
4
|
+
data.tar.gz: e80c4957c57cc26a7608fb9e40d877525513bd44
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b19f3d7791769e403a280a21cea95c9fa2143a7af369eccfd3d84d0183e44012acd512833b654a0a32c4ba6d1400979cd7679982011195ec31dbea17ba249770
|
7
|
+
data.tar.gz: b71a8574acccce1523c931e7c39776e687efcffa7468f12184683a3aa8b2b21087b5828943928081f12921f57f87d5d2f50df7f1ba4c6f580255a6dfa25d1ebd
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Cruddy'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
data/lib/cruddy.rb
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
module Cruddy::Controller
|
2
|
+
module ClassMethods
|
3
|
+
|
4
|
+
end
|
5
|
+
|
6
|
+
module InstanceMethods
|
7
|
+
# the class this controller manages
|
8
|
+
def resource_class
|
9
|
+
controller_name.classify.constantize
|
10
|
+
end
|
11
|
+
|
12
|
+
# the name of the resource collection
|
13
|
+
def resource_collection_name
|
14
|
+
controller_name
|
15
|
+
end
|
16
|
+
|
17
|
+
# the name of the singular resource
|
18
|
+
def resource_instance_name
|
19
|
+
controller_name.singularize
|
20
|
+
end
|
21
|
+
|
22
|
+
# get instance variable for singluar resource instance
|
23
|
+
def resource
|
24
|
+
instance_variable_get("@#{resource_instance_name}")
|
25
|
+
end
|
26
|
+
|
27
|
+
# set instance variable for singluar resource instance
|
28
|
+
def resource=(resource)
|
29
|
+
instance_variable_set("@#{resource_instance_name}", resource)
|
30
|
+
end
|
31
|
+
|
32
|
+
# get instance variable for collection of resource instances
|
33
|
+
def resources
|
34
|
+
instance_variable_get("@#{resource_collection_name}")
|
35
|
+
end
|
36
|
+
|
37
|
+
# set instance variable for collection of resource instances
|
38
|
+
def resources=(collection)
|
39
|
+
instance_variable_set("@#{resource_collection_name}", collection)
|
40
|
+
end
|
41
|
+
|
42
|
+
# require the resource and permit all params by default
|
43
|
+
def resource_params
|
44
|
+
params.require(resource_instance_name).permit!
|
45
|
+
end
|
46
|
+
|
47
|
+
# get the path to a singular resource
|
48
|
+
def resource_path(resource)
|
49
|
+
send("#{resource_instance_name}_path", resource)
|
50
|
+
end
|
51
|
+
|
52
|
+
# get the path to index resources
|
53
|
+
def resources_path
|
54
|
+
send("#{resource_collection_name}_path")
|
55
|
+
end
|
56
|
+
|
57
|
+
def find_resource_instance
|
58
|
+
self.resource = resource_class.find(params[:id])
|
59
|
+
end
|
60
|
+
|
61
|
+
def find_resource_collection
|
62
|
+
self.resources = resource_class.all
|
63
|
+
end
|
64
|
+
|
65
|
+
# index action
|
66
|
+
def index
|
67
|
+
find_resource_collection
|
68
|
+
respond_with resources
|
69
|
+
end
|
70
|
+
|
71
|
+
# show action
|
72
|
+
def show
|
73
|
+
find_resource_instance
|
74
|
+
respond_with resource
|
75
|
+
end
|
76
|
+
|
77
|
+
# new action
|
78
|
+
def new
|
79
|
+
self.resource = resource_class.new
|
80
|
+
respond_with resource
|
81
|
+
end
|
82
|
+
|
83
|
+
# create action
|
84
|
+
def create
|
85
|
+
self.resource = resource_class.new(resource_params)
|
86
|
+
|
87
|
+
if resource.save
|
88
|
+
redirect_to resource_path(resource)
|
89
|
+
else
|
90
|
+
respond_with(resource) do |format|
|
91
|
+
format.html { render :new }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# edit action
|
97
|
+
def edit
|
98
|
+
find_resource_instance
|
99
|
+
respond_with resource
|
100
|
+
end
|
101
|
+
|
102
|
+
# update action
|
103
|
+
def update
|
104
|
+
find_resource_instance
|
105
|
+
|
106
|
+
if resource.update_attributes(resource_params)
|
107
|
+
redirect_to resource_path(resource)
|
108
|
+
else
|
109
|
+
respond_with(resource) do |format|
|
110
|
+
format.html { render :edit }
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# destroy action
|
116
|
+
def destroy
|
117
|
+
find_resource_instance
|
118
|
+
resource.destroy
|
119
|
+
redirect_to resources_path
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def self.included(receiver)
|
124
|
+
receiver.extend ClassMethods
|
125
|
+
receiver.send :include, InstanceMethods
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples "cruddy::controller#create" do
|
4
|
+
let(:resource_instance_name) { resource_class.to_s.downcase }
|
5
|
+
let(:resource_collection_name) { resource_instance_name.pluralize }
|
6
|
+
|
7
|
+
describe "POST create" do
|
8
|
+
context "when saved successfully" do
|
9
|
+
let(:instance) { mock_model resource_class, valid_resource_params.merge({save: true}) }
|
10
|
+
let(:instance_path_method) { "#{resource_instance_name}_path" }
|
11
|
+
let(:instance_path) { send(instance_path_method, instance) }
|
12
|
+
|
13
|
+
before do
|
14
|
+
resource_class.stub(:new).with(valid_resource_params) { instance }
|
15
|
+
end
|
16
|
+
|
17
|
+
it "assigns the newly created resource" do
|
18
|
+
post :create, resource_instance_name => valid_resource_params
|
19
|
+
assigns(resource_instance_name).should eq(instance)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "redirects to the newly created resource" do
|
23
|
+
post :create, resource_instance_name => valid_resource_params
|
24
|
+
response.should redirect_to(instance_path)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when save fails" do
|
29
|
+
let(:instance) { mock_model resource_class, valid_resource_params.merge({save: false}) }
|
30
|
+
let(:instance_path_method) { "#{resource_instance_name}_path" }
|
31
|
+
|
32
|
+
before do
|
33
|
+
resource_class.stub(:new).with(valid_resource_params) { instance }
|
34
|
+
end
|
35
|
+
|
36
|
+
it "assigns the newly created resource" do
|
37
|
+
post :create, resource_instance_name => valid_resource_params
|
38
|
+
assigns(resource_instance_name).should eq(instance)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "renders the new view" do
|
42
|
+
post :create, resource_instance_name => valid_resource_params
|
43
|
+
response.should render_template(:new)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples "cruddy::controller" do
|
4
|
+
# Create
|
5
|
+
it_behaves_like "cruddy::controller#new"
|
6
|
+
it_behaves_like "cruddy::controller#create"
|
7
|
+
|
8
|
+
# Read
|
9
|
+
it_behaves_like "cruddy::controller#index"
|
10
|
+
it_behaves_like "cruddy::controller#show"
|
11
|
+
|
12
|
+
# Update
|
13
|
+
it_behaves_like "cruddy::controller#edit"
|
14
|
+
it_behaves_like "cruddy::controller#update"
|
15
|
+
|
16
|
+
# Delete
|
17
|
+
it_behaves_like "cruddy::controller#destroy"
|
18
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples "cruddy::controller#destroy" do
|
4
|
+
let(:resource_instance_name) { resource_class.to_s.downcase }
|
5
|
+
let(:resource_collection_name) { resource_instance_name.pluralize }
|
6
|
+
|
7
|
+
describe "DELETE destroy" do
|
8
|
+
let(:instance) { mock_model resource_class, destroy: true }
|
9
|
+
let(:collection_path_method) { "#{resource_collection_name}_path" }
|
10
|
+
let(:collection_path) { send(collection_path_method) }
|
11
|
+
|
12
|
+
before do
|
13
|
+
resource_class.stub(:find).with(instance.id.to_s) { instance }
|
14
|
+
end
|
15
|
+
|
16
|
+
it "finds the resource instance" do
|
17
|
+
delete :destroy, id: instance.id
|
18
|
+
assigns(resource_instance_name).should eq(instance)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "destroys the resource" do
|
22
|
+
delete :destroy, id: instance.id
|
23
|
+
expect(instance).to have_received(:destroy)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "redirects to the resource collection path" do
|
27
|
+
delete :destroy, id: instance.id
|
28
|
+
response.should redirect_to(collection_path)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples "cruddy::controller#edit" do
|
4
|
+
let(:resource_instance_name) { resource_class.to_s.downcase }
|
5
|
+
let(:resource_collection_name) { resource_instance_name.pluralize }
|
6
|
+
|
7
|
+
describe "GET edit" do
|
8
|
+
let(:instance) { mock_model resource_class }
|
9
|
+
|
10
|
+
before do
|
11
|
+
resource_class.stub(:find).with(instance.id.to_s) { instance }
|
12
|
+
end
|
13
|
+
|
14
|
+
it "finds the instance" do
|
15
|
+
get :edit, id: instance.id
|
16
|
+
assigns(resource_instance_name).should eq(instance)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "renders the edit view" do
|
20
|
+
get :edit, id: instance.id
|
21
|
+
response.should render_template(:edit)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples "cruddy::controller#index" do
|
4
|
+
let(:resource_instance_name) { resource_class.to_s.downcase }
|
5
|
+
let(:resource_collection_name) { resource_instance_name.pluralize }
|
6
|
+
|
7
|
+
describe "GET index" do
|
8
|
+
let(:instance) { mock_model resource_class }
|
9
|
+
|
10
|
+
before do
|
11
|
+
resource_class.stub(:all) { [instance] }
|
12
|
+
end
|
13
|
+
|
14
|
+
it "finds the collection" do
|
15
|
+
get :index
|
16
|
+
assigns(resource_collection_name).should eq(resource_class.all)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "renders the index view" do
|
20
|
+
get :index
|
21
|
+
response.should render_template(:index)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples "cruddy::controller#new" do
|
4
|
+
let(:resource_instance_name) { resource_class.to_s.downcase }
|
5
|
+
let(:resource_collection_name) { resource_instance_name.pluralize }
|
6
|
+
|
7
|
+
describe "GET new" do
|
8
|
+
it "initializes a resource instance" do
|
9
|
+
get :new
|
10
|
+
assigns(resource_instance_name).should be_a_kind_of(resource_class)
|
11
|
+
assigns(resource_instance_name).should be_new_record
|
12
|
+
end
|
13
|
+
|
14
|
+
it "renders the new view" do
|
15
|
+
get :new
|
16
|
+
response.should render_template(:new)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples "cruddy::controller#show" do
|
4
|
+
let(:resource_instance_name) { resource_class.to_s.downcase }
|
5
|
+
let(:resource_collection_name) { resource_instance_name.pluralize }
|
6
|
+
|
7
|
+
describe "GET show" do
|
8
|
+
let(:instance) { mock_model resource_class }
|
9
|
+
|
10
|
+
before do
|
11
|
+
resource_class.stub(:find).with(instance.id.to_s) { instance }
|
12
|
+
end
|
13
|
+
|
14
|
+
it "finds the instance" do
|
15
|
+
get :show, id: instance.id
|
16
|
+
assigns(resource_instance_name).should eq(instance)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "renders the show view" do
|
20
|
+
get :show, id: instance.id
|
21
|
+
response.should render_template(:show)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples "cruddy::controller#update" do
|
4
|
+
let(:resource_instance_name) { resource_class.to_s.downcase }
|
5
|
+
let(:resource_collection_name) { resource_instance_name.pluralize }
|
6
|
+
|
7
|
+
describe "PATCH update" do
|
8
|
+
let(:instance) { mock_model resource_class, update_attributes: true }
|
9
|
+
let(:instance_path_method) { "#{resource_instance_name}_path" }
|
10
|
+
let(:instance_path) { send(instance_path_method, instance) }
|
11
|
+
|
12
|
+
before do
|
13
|
+
resource_class.stub(:find).with(instance.id.to_s) { instance }
|
14
|
+
end
|
15
|
+
|
16
|
+
it "finds the instance" do
|
17
|
+
patch :update, id: instance.id, resource_instance_name => valid_resource_params
|
18
|
+
assigns(resource_instance_name).should eq(instance)
|
19
|
+
end
|
20
|
+
|
21
|
+
context "when saved successfully" do
|
22
|
+
before do
|
23
|
+
instance.stub(:update_attributes).with(valid_resource_params) { true }
|
24
|
+
end
|
25
|
+
|
26
|
+
it "redirects to the newly created resource" do
|
27
|
+
patch :update, id: instance.id, resource_instance_name => valid_resource_params
|
28
|
+
response.should redirect_to(instance_path)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "when save fails" do
|
33
|
+
before do
|
34
|
+
instance.stub(:update_attributes).with(valid_resource_params) { false }
|
35
|
+
end
|
36
|
+
|
37
|
+
it "renders the edit view" do
|
38
|
+
patch :update, id: instance.id, resource_instance_name => valid_resource_params
|
39
|
+
response.should render_template(:edit)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cruddy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Rehberg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.14'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.14'
|
55
|
+
description: Cruddy is intended to DRY up basic CRUD in rails controllers.
|
56
|
+
email:
|
57
|
+
- hi@alexrehberg.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- MIT-LICENSE
|
63
|
+
- Rakefile
|
64
|
+
- lib/cruddy.rb
|
65
|
+
- lib/cruddy/controller.rb
|
66
|
+
- lib/cruddy/spec_support.rb
|
67
|
+
- lib/cruddy/spec_support/create_examples.rb
|
68
|
+
- lib/cruddy/spec_support/cruddy_examples.rb
|
69
|
+
- lib/cruddy/spec_support/destroy_examples.rb
|
70
|
+
- lib/cruddy/spec_support/edit_examples.rb
|
71
|
+
- lib/cruddy/spec_support/index_examples.rb
|
72
|
+
- lib/cruddy/spec_support/new_examples.rb
|
73
|
+
- lib/cruddy/spec_support/show_examples.rb
|
74
|
+
- lib/cruddy/spec_support/update_examples.rb
|
75
|
+
- lib/cruddy/version.rb
|
76
|
+
- lib/tasks/cruddy_tasks.rake
|
77
|
+
homepage: http://github.com/arehberg/cruddy
|
78
|
+
licenses: []
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.2.0
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Basic crud stuff for rails
|
100
|
+
test_files: []
|