resourceful_controller 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/lib/resourceful_controller.rb +34 -0
- data/lib/resourceful_controller/crud_definitions.rb +79 -0
- data/lib/resourceful_controller/query_definitions.rb +30 -0
- data/lib/resourceful_controller/resource_identification.rb +23 -0
- data/lib/resourceful_controller/url_definitions.rb +15 -0
- data/lib/resourceful_controller/version.rb +3 -0
- data/resourceful_controller.gemspec +24 -0
- data/spec/resourceful_controller_spec.rb +109 -0
- metadata +103 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
|
4
|
+
require 'resourceful_controller/resource_identification'
|
5
|
+
require 'resourceful_controller/query_definitions'
|
6
|
+
require 'resourceful_controller/url_definitions'
|
7
|
+
require 'resourceful_controller/crud_definitions'
|
8
|
+
|
9
|
+
module ResourcefulController
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
include ActiveSupport
|
12
|
+
|
13
|
+
include ResourceIdentification
|
14
|
+
include QueryDefinitions
|
15
|
+
include UrlDefinitions
|
16
|
+
|
17
|
+
delegate :options, :to => :klass
|
18
|
+
|
19
|
+
def klass
|
20
|
+
self.class
|
21
|
+
end
|
22
|
+
|
23
|
+
module ClassMethods
|
24
|
+
include CrudDefinitions
|
25
|
+
|
26
|
+
attr_accessor :options
|
27
|
+
|
28
|
+
def act_resourcefully options = {}
|
29
|
+
self.options = options
|
30
|
+
define_crud_methods
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module ResourcefulController
|
2
|
+
module CrudDefinitions
|
3
|
+
def define_crud_methods
|
4
|
+
methods.each { |method| send(:"define_#{method}") }
|
5
|
+
end
|
6
|
+
|
7
|
+
def methods
|
8
|
+
methods = default_methods - excepted_methods
|
9
|
+
methods = methods & specified_methods if specified_methods.present?
|
10
|
+
return methods
|
11
|
+
end
|
12
|
+
|
13
|
+
def excepted_methods
|
14
|
+
Array.wrap(@options[:except])
|
15
|
+
end
|
16
|
+
|
17
|
+
def specified_methods
|
18
|
+
Array.wrap(@options[:only])
|
19
|
+
end
|
20
|
+
|
21
|
+
def default_methods
|
22
|
+
[:index, :new, :create, :show, :edit, :update, :destroy]
|
23
|
+
end
|
24
|
+
|
25
|
+
def define_index
|
26
|
+
define_method :index do
|
27
|
+
instance_variable_set(:"@#{resources_name}", _index_query.call(params))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def define_new
|
32
|
+
define_method :new do
|
33
|
+
instance_variable_set(:"@#{resource_name}", resource_class.new)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def define_create
|
38
|
+
define_method :create do
|
39
|
+
instance_variable_set(:"@#{resource_name}", resource_class.new(params[:"#{resource_name}"]))
|
40
|
+
if instance_variable_get(:"@#{resource_name}").save
|
41
|
+
send(:redirect_to, show_path, :notice => "#{resource_name.titleize.capitalize} successfully created")
|
42
|
+
else
|
43
|
+
render :new
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def define_show
|
49
|
+
define_method :show do
|
50
|
+
instance_variable_set(:"@#{resource_name}", _find_query.call(params))
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def define_edit
|
55
|
+
define_method :edit do
|
56
|
+
instance_variable_set(:"@#{resource_name}", _find_query.call(params))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def define_update
|
61
|
+
define_method :update do
|
62
|
+
instance_variable_set(:"@#{resource_name}", _find_query.call(params))
|
63
|
+
if instance_variable_get(:"@#{resource_name}").update_attributes(params[:"#{resource_name}"])
|
64
|
+
redirect_to show_path, :notice => "#{resource_name.titleize.capitalize} successfully updated"
|
65
|
+
else
|
66
|
+
render :edit
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def define_destroy
|
72
|
+
define_method :destroy do
|
73
|
+
resource = _find_query.call(params)
|
74
|
+
resource.destroy
|
75
|
+
redirect_to index_path, :notice => "#{resource_name.titleize.capitalize} successfully deleted"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ResourcefulController
|
2
|
+
module QueryDefinitions
|
3
|
+
QUERY_METHODS = [:index_query, :find_query, :show_query, :edit_query, :update_query, :destroy_query]
|
4
|
+
|
5
|
+
QUERY_METHODS.each do |query_method|
|
6
|
+
define_method :"_#{query_method}" do
|
7
|
+
procify(options[query_method]) ||
|
8
|
+
send(:"default_#{query_method}")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def default_index_query
|
13
|
+
lambda { |params| resource_class.all }
|
14
|
+
end
|
15
|
+
|
16
|
+
def default_find_query
|
17
|
+
lambda { |params| resource_class.find(params[:id]) }
|
18
|
+
end
|
19
|
+
|
20
|
+
(QUERY_METHODS - [:index_query, :find_query]).each do |query_method|
|
21
|
+
alias :"default_#{query_method}" :default_find_query
|
22
|
+
end
|
23
|
+
|
24
|
+
def procify option
|
25
|
+
return unless option
|
26
|
+
option.is_a?(Proc) ? option : Proc.new { send option }
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ResourcefulController
|
2
|
+
module ResourceIdentification
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
delegate :resource_name, :resources_name, :resource_class, :to => :klass
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
def resource_name
|
10
|
+
resources_name.singularize
|
11
|
+
end
|
12
|
+
|
13
|
+
def resources_name
|
14
|
+
controller_name
|
15
|
+
end
|
16
|
+
|
17
|
+
def resource_class
|
18
|
+
resource_name.classify.constantize
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ResourcefulController
|
2
|
+
module UrlDefinitions
|
3
|
+
def namespace
|
4
|
+
controller_path.split("/")[0..-2]
|
5
|
+
end
|
6
|
+
|
7
|
+
def index_path
|
8
|
+
namespace.push(resources_name)
|
9
|
+
end
|
10
|
+
|
11
|
+
def show_path
|
12
|
+
namespace.push(instance_variable_get(:"@#{resource_name}"))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "resourceful_controller/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "resourceful_controller"
|
7
|
+
s.version = ResourcefulController::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Ben Eddy"]
|
10
|
+
s.email = ["bae@foraker.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Adds standard REST actions to a controller}
|
13
|
+
s.description = %q{Adds standard REST actions to a controller}
|
14
|
+
|
15
|
+
s.rubyforge_project = "resourceful_controller"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency "rails"
|
23
|
+
s.add_development_dependency "rspec-rails"
|
24
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'resourceful_controller'
|
2
|
+
require 'rails'
|
3
|
+
require 'action_controller'
|
4
|
+
require 'rspec/rails'
|
5
|
+
|
6
|
+
class Application < Rails::Application
|
7
|
+
end
|
8
|
+
|
9
|
+
class TestController < ActionController::Base
|
10
|
+
include ResourcefulController
|
11
|
+
end
|
12
|
+
|
13
|
+
describe TestController do
|
14
|
+
extend ActionController::TestCase::Behavior
|
15
|
+
include RSpec::Rails::ControllerExampleGroup
|
16
|
+
|
17
|
+
describe "resourceful methods" do
|
18
|
+
before do
|
19
|
+
[:index, :new, :create, :show, :edit, :update, :destroy].each do |method|
|
20
|
+
TestController.send(:remove_method, method) if TestController.new.respond_to?(method)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "adds all resourceful actions" do
|
25
|
+
TestController.act_resourcefully
|
26
|
+
|
27
|
+
[:index, :new, :create, :show, :edit, :update, :destroy].each do |method|
|
28
|
+
controller.respond_to?(method).should be_true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it "removes excepted methods" do
|
33
|
+
TestController.act_resourcefully :except => :destroy
|
34
|
+
|
35
|
+
[:index, :new, :create, :show, :edit, :update].each do |method|
|
36
|
+
controller.respond_to?(method).should be_true
|
37
|
+
end
|
38
|
+
|
39
|
+
controller.respond_to?(:destroy).should be_false
|
40
|
+
end
|
41
|
+
|
42
|
+
it "only adds specified actions" do
|
43
|
+
TestController.act_resourcefully :only => :destroy
|
44
|
+
|
45
|
+
controller.respond_to?(:destroy).should be_true
|
46
|
+
|
47
|
+
[:index, :new, :create, :show, :edit, :update].each do |method|
|
48
|
+
controller.respond_to?(method).should be_false
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "support methods" do
|
54
|
+
before { TestController.act_resourcefully }
|
55
|
+
|
56
|
+
describe "#resource_name" do
|
57
|
+
it "infers the resource name from the controller class" do
|
58
|
+
controller.resource_name.should == "test"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "#resource_class" do
|
63
|
+
it "infers the resource class from the controller class" do
|
64
|
+
controller.resource_class.should == Test
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "query methods" do
|
69
|
+
let(:query_methods) do
|
70
|
+
[:index_query, :find_query, :show_query, :edit_query, :update_query, :destroy_query]
|
71
|
+
end
|
72
|
+
|
73
|
+
it "defines default query methods" do
|
74
|
+
query_methods.each do |query_method|
|
75
|
+
controller.respond_to?(:"default_#{query_method}").should be_true
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
it "returns the default query" do
|
80
|
+
query = Proc.new { "oncilla" }
|
81
|
+
|
82
|
+
query_methods.each do |query_method|
|
83
|
+
controller.stub(:"default_#{query_method}" => query)
|
84
|
+
controller.send(:"_#{query_method}").call.should == "oncilla"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
it "defers to a supplied proc" do
|
89
|
+
query_methods.each do |query_method|
|
90
|
+
TestController.act_resourcefully query_method => lambda { |params| params }
|
91
|
+
controller.send(:"_#{query_method}").call("margay").should == "margay"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
it "defers to a supplied method name" do
|
96
|
+
class TestController
|
97
|
+
def my_query ; "margay" ; end
|
98
|
+
end
|
99
|
+
|
100
|
+
query_methods.each do |query_method|
|
101
|
+
TestController.act_resourcefully query_method => :my_query
|
102
|
+
controller.send(:"_#{query_method}").call.should == "margay"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resourceful_controller
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ben Eddy
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-11-06 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rails
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rspec-rails
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
description: Adds standard REST actions to a controller
|
49
|
+
email:
|
50
|
+
- bae@foraker.com
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files: []
|
56
|
+
|
57
|
+
files:
|
58
|
+
- .gitignore
|
59
|
+
- Gemfile
|
60
|
+
- Rakefile
|
61
|
+
- lib/resourceful_controller.rb
|
62
|
+
- lib/resourceful_controller/crud_definitions.rb
|
63
|
+
- lib/resourceful_controller/query_definitions.rb
|
64
|
+
- lib/resourceful_controller/resource_identification.rb
|
65
|
+
- lib/resourceful_controller/url_definitions.rb
|
66
|
+
- lib/resourceful_controller/version.rb
|
67
|
+
- resourceful_controller.gemspec
|
68
|
+
- spec/resourceful_controller_spec.rb
|
69
|
+
homepage: ""
|
70
|
+
licenses: []
|
71
|
+
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
hash: 3
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
hash: 3
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project: resourceful_controller
|
98
|
+
rubygems_version: 1.8.8
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: Adds standard REST actions to a controller
|
102
|
+
test_files:
|
103
|
+
- spec/resourceful_controller_spec.rb
|