restful-controller 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ require 'rails/all'
1
2
  require 'restful_controller/action_controller/base'
2
3
 
3
4
  require 'restful_controller/base'
@@ -1,5 +1,7 @@
1
- class ActionController::Base
2
- def self.restful_controller(*actions)
3
- RestfulController::Base.inject(self, actions)
1
+ module ActionController
2
+ class Base
3
+ def self.restful_controller(*actions)
4
+ RestfulController::Base.inject(self, *actions)
5
+ end
4
6
  end
5
7
  end
@@ -2,7 +2,13 @@ module RestfulController
2
2
  module Actions
3
3
  module Create
4
4
  def create
5
- self.class.model_class.create(model_params)
5
+ model = self.class.model_class.build(model_params)
6
+ instance_variable_set("@#{self.class.model_name}", model)
7
+ if model.save
8
+ redirect_to model, notice: "#{model.name} was successfully created."
9
+ else
10
+ render action: 'new'
11
+ end
6
12
  end
7
13
  end
8
14
  end
@@ -2,7 +2,7 @@ module RestfulController
2
2
  module Actions
3
3
  module Destroy
4
4
  def destroy
5
- instance_variable_get("@#{self.class.model_name.pluralize}").destroy
5
+ instance_variable_get("@#{self.class.model_name}").destroy
6
6
  end
7
7
  end
8
8
  end
@@ -2,7 +2,7 @@ module RestfulController
2
2
  module Actions
3
3
  module New
4
4
  def new
5
- model = self.class.model_class.new
5
+ model = self.class.model_class.build
6
6
  instance_variable_set("@#{self.class.model_name}", model)
7
7
  end
8
8
  end
@@ -2,7 +2,11 @@ module RestfulController
2
2
  module Actions
3
3
  module Update
4
4
  def update
5
- instance_variable_get("@#{self.class.model_name.pluralize}").update_attributes(model_params)
5
+ if instance_variable_get("@#{self.class.model_name}").update_attributes(model_params)
6
+ redirect_to instance_variable_get("@#{self.class.model_name}"), notice: "Was successfully updated."
7
+ else
8
+ render action: 'edit'
9
+ end
6
10
  end
7
11
  end
8
12
  end
@@ -2,7 +2,7 @@ module RestfulController
2
2
  class Base
3
3
  ACTIONS = [:index, :show, :edit, :update, :create, :destroy, :new]
4
4
 
5
- def self.inject(base, actions = nil)
5
+ def self.inject(base, actions = [])
6
6
  actions_to_include = actions_to_include(actions)
7
7
  base.class_eval do
8
8
  include Helpers
@@ -19,11 +19,10 @@ module RestfulController
19
19
  ACTIONS
20
20
  elsif actions.length == 1 && actions.first.is_a?(Hash)
21
21
  hash = actions.first
22
- key = hash.keys.first
23
- if key == :except
24
- ACTIONS - Array(hash[key])
25
- elsif key == :only
26
- Array(hash[key])
22
+ if hash[:except]
23
+ ACTIONS - Array(hash[:except])
24
+ elsif hash[:only]
25
+ Array(hash[:only])
27
26
  end
28
27
  else
29
28
  actions
@@ -2,6 +2,12 @@ module RestfulController
2
2
  module Filters
3
3
  extend ActiveSupport::Concern
4
4
  included do
5
+ # set_post
6
+ define_method "set_#{model_name}" do
7
+ model = self.class.model_class.find(params[:id])
8
+ instance_variable_set("@#{self.class.model_name}", model)
9
+ end
10
+
5
11
  append_before_filter "set_#{model_name}", only: [:show, :edit, :update, :destroy]
6
12
  end
7
13
  end
@@ -2,14 +2,6 @@ module RestfulController
2
2
  module Helpers
3
3
  extend ActiveSupport::Concern
4
4
 
5
- included do
6
- # set_post
7
- define_method "set_#{model_name}" do
8
- model = self.class.model_class.find(params[:id])
9
- instance_variable_set("@#{self.class.model_name}", model)
10
- end
11
- end
12
-
13
5
  def model_params
14
6
  send("#{self.class.model_name}_params")
15
7
  end
@@ -23,6 +15,5 @@ module RestfulController
23
15
  controller_name.classify.constantize
24
16
  end
25
17
  end
26
-
27
18
  end
28
19
  end
@@ -1,3 +1,3 @@
1
1
  module RestfulController
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -15,8 +15,10 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = RestfulController::VERSION
17
17
 
18
- gem.add_development_dependency 'rspec'
19
18
  gem.add_development_dependency 'rake'
19
+ gem.add_development_dependency 'rspec'
20
+ gem.add_development_dependency 'rspec-rails'
20
21
  gem.add_development_dependency 'pry'
21
- gem.add_development_dependency 'actionpack'
22
+ gem.add_development_dependency 'actionpack', '~> 3.1'
23
+ gem.add_development_dependency 'activesupport', '~> 3.1'
22
24
  end
@@ -0,0 +1,21 @@
1
+ require 'active_support/all'
2
+ require 'action_controller'
3
+ require 'action_dispatch'
4
+
5
+ module Rails
6
+ class App
7
+ def env_config; {} end
8
+ def routes
9
+ return @routes if defined?(@routes)
10
+ @routes = ActionDispatch::Routing::RouteSet.new
11
+ @routes.draw do
12
+ resources :posts
13
+ end
14
+ @routes
15
+ end
16
+ end
17
+
18
+ def self.application
19
+ @app ||= App.new
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ class TestController < ActionController::Base
2
+ include Rails.application.routes.url_helpers
3
+
4
+ def render(*attributes); end
5
+ end
6
+
7
+ class PostsController < TestController
8
+ restful_controller
9
+ end
@@ -0,0 +1,7 @@
1
+ class Post
2
+ attr_accessor :id
3
+
4
+ def initialize
5
+ @id = 1
6
+ end
7
+ end
@@ -1 +1,20 @@
1
- require 'spec_helper'
1
+ require 'spec_helper'
2
+
3
+ describe RestfulController::Base do
4
+ describe '.actions_to_include' do
5
+ it 'accepts array' do
6
+ actions = [:index, :show]
7
+ RestfulController::Base.actions_to_include(actions).should eq([:index, :show])
8
+ end
9
+
10
+ it 'uses :only' do
11
+ actions = [{only: [:index, :show]}]
12
+ RestfulController::Base.actions_to_include(actions).should eq([:index, :show])
13
+ end
14
+
15
+ it 'uses :except' do
16
+ actions = [{except: [:index]}]
17
+ RestfulController::Base.actions_to_include(actions).should eq([:show, :edit, :update, :create, :destroy, :new])
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+ require 'fixtures/application'
3
+ require 'fixtures/controllers'
4
+ require 'fixtures/models'
5
+ require 'rspec/rails'
6
+
7
+ describe PostsController, type: :controller do
8
+ before do
9
+ @post = Post.new
10
+ @post_params = { title: "Title" }
11
+ PostsController.any_instance.stub(:post_params).and_return(@post_params)
12
+ end
13
+
14
+ describe '#show' do
15
+ it "has @post" do
16
+ Post.stub(:find).with(@post.id.to_s).and_return(@post)
17
+ get :show, id: @post.id
18
+ assigns(:post).should eq(@post)
19
+ end
20
+ end
21
+
22
+ describe '#edit' do
23
+ it "has @post" do
24
+ Post.stub(:find).with(@post.id.to_s).and_return(@post)
25
+ get :edit, id: @post.id
26
+ assigns(:post).should eq(@post)
27
+ end
28
+ end
29
+
30
+ describe '#update' do
31
+ it 'updates @post' do
32
+ Post.stub(:find).with(@post.id.to_s).and_return(@post)
33
+
34
+ @post.should_receive(:update_attributes).with(@post_params)
35
+
36
+ put :update, {id: @post.id, post: @post_params }
37
+ end
38
+ end
39
+
40
+ describe '#index' do
41
+ it 'has @posts' do
42
+ Post.stub(:all).and_return([@post])
43
+
44
+ get :index, {id: @post.id}
45
+
46
+ assigns(:posts).should eq([@post])
47
+ end
48
+ end
49
+
50
+ describe '#destroy' do
51
+ it 'destroys @post' do
52
+ Post.stub(:find).with(@post.id.to_s).and_return(@post)
53
+
54
+ @post.should_receive(:destroy)
55
+
56
+ delete :destroy, {id: @post.id}
57
+ end
58
+ end
59
+
60
+ describe '#create' do
61
+ it 'builds and saves @post' do
62
+ Post.should_receive(:build).with(@post_params).and_return(@post)
63
+ @post.should_receive(:save)
64
+ post :create, {id: @post.id, post: @post_params }
65
+ assigns(:post).should eq(@post)
66
+ end
67
+ end
68
+
69
+ describe '#new' do
70
+ it 'builds @post' do
71
+ Post.should_receive(:build).and_return(@post)
72
+ get :new, {id: @post.id}
73
+ assigns(:post).should eq(@post)
74
+ end
75
+ end
76
+ end
@@ -1,3 +1,2 @@
1
- require 'rspec'
2
- require 'pry'
3
1
  require 'restful-controller'
2
+ require 'pry'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restful-controller
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:
@@ -9,10 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-19 00:00:00.000000000 Z
12
+ date: 2013-01-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rspec
15
+ name: rake
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
@@ -28,7 +28,7 @@ dependencies:
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
- name: rake
31
+ name: rspec
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
@@ -44,7 +44,7 @@ dependencies:
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
46
  - !ruby/object:Gem::Dependency
47
- name: pry
47
+ name: rspec-rails
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
@@ -60,7 +60,7 @@ dependencies:
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  - !ruby/object:Gem::Dependency
63
- name: actionpack
63
+ name: pry
64
64
  requirement: !ruby/object:Gem::Requirement
65
65
  none: false
66
66
  requirements:
@@ -75,6 +75,38 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: actionpack
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '3.1'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '3.1'
94
+ - !ruby/object:Gem::Dependency
95
+ name: activesupport
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: '3.1'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: '3.1'
78
110
  description: Restful Controllers for your Rails app
79
111
  email:
80
112
  - alexander@say26.com
@@ -103,7 +135,11 @@ files:
103
135
  - lib/restful_controller/helpers.rb
104
136
  - lib/restful_controller/version.rb
105
137
  - restful-controller.gemspec
138
+ - spec/fixtures/application.rb
139
+ - spec/fixtures/controllers.rb
140
+ - spec/fixtures/models.rb
106
141
  - spec/restful-controller/base_spec.rb
142
+ - spec/restful-controller/controller_spec.rb
107
143
  - spec/spec_helper.rb
108
144
  homepage: https://github.com/AlexanderZaytsev/restful-controller
109
145
  licenses: []
@@ -119,7 +155,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
119
155
  version: '0'
120
156
  segments:
121
157
  - 0
122
- hash: 4425193934035644459
158
+ hash: -3875835659632608554
123
159
  required_rubygems_version: !ruby/object:Gem::Requirement
124
160
  none: false
125
161
  requirements:
@@ -128,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
164
  version: '0'
129
165
  segments:
130
166
  - 0
131
- hash: 4425193934035644459
167
+ hash: -3875835659632608554
132
168
  requirements: []
133
169
  rubyforge_project:
134
170
  rubygems_version: 1.8.24
@@ -136,5 +172,9 @@ signing_key:
136
172
  specification_version: 3
137
173
  summary: It's like scaffolding, but invisible
138
174
  test_files:
175
+ - spec/fixtures/application.rb
176
+ - spec/fixtures/controllers.rb
177
+ - spec/fixtures/models.rb
139
178
  - spec/restful-controller/base_spec.rb
179
+ - spec/restful-controller/controller_spec.rb
140
180
  - spec/spec_helper.rb