resource_full 0.7.6

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.
Files changed (35) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +100 -0
  3. data/Rakefile +19 -0
  4. data/lib/resource_full/base.rb +140 -0
  5. data/lib/resource_full/controllers/resources.rb +16 -0
  6. data/lib/resource_full/controllers/resources_controller.rb +26 -0
  7. data/lib/resource_full/controllers/routes_controller.rb +16 -0
  8. data/lib/resource_full/core_extensions/api.rb +26 -0
  9. data/lib/resource_full/core_extensions/exception.rb +25 -0
  10. data/lib/resource_full/core_extensions/from_json.rb +13 -0
  11. data/lib/resource_full/core_extensions/module.rb +13 -0
  12. data/lib/resource_full/dispatch.rb +196 -0
  13. data/lib/resource_full/models/resourced_route.rb +84 -0
  14. data/lib/resource_full/query.rb +337 -0
  15. data/lib/resource_full/render/html.rb +74 -0
  16. data/lib/resource_full/render/json.rb +107 -0
  17. data/lib/resource_full/render/xml.rb +106 -0
  18. data/lib/resource_full/render.rb +63 -0
  19. data/lib/resource_full/retrieve.rb +87 -0
  20. data/lib/resource_full/version.rb +9 -0
  21. data/lib/resource_full.rb +31 -0
  22. data/spec/resource_full/base_spec.rb +88 -0
  23. data/spec/resource_full/controllers/resources_spec.rb +30 -0
  24. data/spec/resource_full/dispatch_spec.rb +274 -0
  25. data/spec/resource_full/models/resourced_route_spec.rb +62 -0
  26. data/spec/resource_full/query/parameter_spec.rb +61 -0
  27. data/spec/resource_full/query_spec.rb +462 -0
  28. data/spec/resource_full/render/html_spec.rb +4 -0
  29. data/spec/resource_full/render/json_spec.rb +258 -0
  30. data/spec/resource_full/render/xml_spec.rb +378 -0
  31. data/spec/resource_full/render_spec.rb +5 -0
  32. data/spec/resource_full/retrieve_spec.rb +184 -0
  33. data/spec/spec.opts +4 -0
  34. data/spec/spec_helper.rb +134 -0
  35. metadata +156 -0
@@ -0,0 +1,184 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "ResourceFull::Retrieve", :type => :controller do
4
+ controller_name "resource_full_mock_users"
5
+
6
+ before :each do
7
+ ResourceFullMockUser.delete_all
8
+ ResourceFullMockUsersController.resource_identifier = :id
9
+ ResourceFullMockUsersController.queryable_params = []
10
+ end
11
+
12
+ it "defines custom methods based on the class name" do
13
+ controller.should respond_to(
14
+ :find_resource_full_mock_user,
15
+ :find_all_resource_full_mock_users,
16
+ :update_resource_full_mock_user,
17
+ :destroy_resource_full_mock_user,
18
+ :new_resource_full_mock_user
19
+ )
20
+ end
21
+
22
+ it "changes the custom methods if the model name is changed" do
23
+ controller.class.exposes ResourceFullMock
24
+ controller.should respond_to(:find_resource_full_mock)
25
+ controller.should_not respond_to(:find_resource_full_mock_user)
26
+ controller.class.exposes ResourceFullMockUser # cleanup
27
+ end
28
+
29
+ it "finds the requested model object" do
30
+ user = ResourceFullMockUser.create!
31
+ get :show, :id => user.id
32
+ assigns(:resource_full_mock_user).should == user
33
+ end
34
+
35
+ describe "resource_identifier and identified_by" do
36
+ controller_name "resource_full_mock_users"
37
+
38
+ before :each do
39
+ ResourceFullMockUser.delete_all
40
+ ResourceFullMockUsersController.resource_identifier = :id
41
+ end
42
+
43
+ def get_should_retrieve_by_first_name
44
+ eustace = ResourceFullMockUser.create! :first_name => "eustace"
45
+ eustace_noise = ResourceFullMockUser.create! :first_name => eustace.id
46
+ get :show, :id => "eustace"
47
+ assigns(:resource_full_mock_user).should == eustace
48
+ end
49
+
50
+ def get_should_retrieve_by_id
51
+ jimbo = ResourceFullMockUser.create! :first_name => "jimbo"
52
+ jimbo_noise = ResourceFullMockUser.create! :first_name => jimbo.id
53
+ get :show, :id => jimbo.id
54
+ assigns(:resource_full_mock_user).should == jimbo
55
+ end
56
+
57
+ it "finds the requested model object using the correct column if the resource_identifier attribute has been overridden" do
58
+ ResourceFullMockUsersController.resource_identifier = :first_name
59
+ get_should_retrieve_by_first_name
60
+ end
61
+
62
+ it "doesn't find the requested model object if there are none to find" do
63
+ ResourceFullMockUsersController.resource_identifier = :first_name
64
+ get :show, :id => "eustace"
65
+ assigns(:resource_full_mock_user).should be_nil
66
+ end
67
+
68
+ it "finds the requested model object using the correct column if a block is provided to resource_identifier" do
69
+ ResourceFullMockUsersController.resource_identifier = lambda do |id|
70
+ if id =~ /[0-9]+/
71
+ :id
72
+ else :first_name end
73
+ end
74
+ get_should_retrieve_by_first_name
75
+ get_should_retrieve_by_id
76
+ end
77
+
78
+ it "finds the requested model object only if the optional block provided to identifed_by is true" do
79
+ ResourceFullMockUsersController.identified_by :first_name, :if => lambda { |id| id =~ /[a-zA-Z]+/ }
80
+ get_should_retrieve_by_first_name
81
+ get_should_retrieve_by_id
82
+ end
83
+
84
+ it "finds the requested model object unless the optional block provided to identifed_by is true" do
85
+ ResourceFullMockUsersController.identified_by :first_name, :unless => lambda { |id| id =~ /[0-9]+/ }
86
+ get_should_retrieve_by_first_name
87
+ get_should_retrieve_by_id
88
+ end
89
+
90
+ it "finds the requested model object unless it requests a fallback to a numeric id" do
91
+ ResourceFullMockUsersController.identified_by :first_name, :unless => :id_numeric
92
+ get_should_retrieve_by_first_name
93
+ get_should_retrieve_by_id
94
+ end
95
+ end
96
+
97
+ describe "update" do
98
+ it "updates the requested model object based on the given parameters" do
99
+ user = ResourceFullMockUser.create! :last_name => "threepwood"
100
+ post :update, :id => user.id, :resource_full_mock_user => { :last_name => "guybrush" }
101
+ user.reload.last_name.should == "guybrush"
102
+ end
103
+
104
+ it "updates the requested model object using the correct column if the resource_identifier attribute has been overridden" do
105
+ ResourceFullMockUsersController.resource_identifier = :first_name
106
+ user = ResourceFullMockUser.create! :first_name => "guybrush"
107
+ post :update, :id => "guybrush", :resource_full_mock_user => { :last_name => "threepwood" }
108
+ user.reload.last_name.should == "threepwood"
109
+ end
110
+
111
+ it "should perform the actions in a transaction (in case the update on the model creates associated/child objects)"
112
+ end
113
+
114
+ describe "create" do
115
+ it "creates a new model object based on the given parameters" do
116
+ put :create, :resource_full_mock_user => { :first_name => "guybrush", :last_name => "threepwood" }
117
+ ResourceFullMockUser.count.should == 1
118
+ ResourceFullMockUser.find(:first).first_name.should == "guybrush"
119
+ end
120
+
121
+ it "creates a new model object appropriately if a creational parameter is queryable but not placed in the model object params, as with a nested route" do
122
+ ResourceFullMockUsersController.queryable_with :first_name
123
+ put :create, :first_name => "guybrush", :resource_full_mock_user => { :last_name => "threepwood" }
124
+ ResourceFullMockUser.count.should == 1
125
+ user = ResourceFullMockUser.find :first
126
+ user.first_name.should == "guybrush"
127
+ user.last_name.should == "threepwood"
128
+ end
129
+
130
+ it "should perform the actions in a transaction (in case the create on the model creates associated/child objects)"
131
+ end
132
+
133
+ describe "destroy" do
134
+ it "deletes the requested model object" do
135
+ user = ResourceFullMockUser.create!
136
+ delete :destroy, :id => user.id
137
+ ResourceFullMockUser.exists?(user.id).should be_false
138
+ end
139
+
140
+ it "deletes the requested model object using the correct column if the resource_identifier attribute has been overridden" do
141
+ ResourceFullMockUsersController.resource_identifier = :first_name
142
+ user = ResourceFullMockUser.create! :first_name => "guybrush"
143
+ delete :destroy, :id => "guybrush"
144
+ ResourceFullMockUser.exists?(user.id).should be_false
145
+ end
146
+
147
+ it "should perform the actions in a transaction (in case the destroy on the model destroys associated/child objects)"
148
+ end
149
+
150
+ describe "with pagination" do
151
+ controller_name "resource_full_mock_users"
152
+
153
+ before :each do
154
+ ResourceFullMockUser.delete_all
155
+ @users = (1..6).collect { ResourceFullMockUser.create! }
156
+ end
157
+
158
+ after :all do
159
+ ResourceFullMockUser.delete_all
160
+ end
161
+
162
+ it "limits the query to the correct number of records given that parameter" do
163
+ ResourceFullMockUsersController.queryable_with :limit, :scope => lambda {|limit| { :limit => limit }}
164
+ get :index, :limit => 2
165
+ assigns(:resource_full_mock_users).should == @users[0..1]
166
+ end
167
+
168
+ it "offsets the query by the correct number of records" do
169
+ ResourceFullMockUsersController.queryable_with :limit, :scope => lambda {|limit| { :limit => limit }}
170
+ ResourceFullMockUsersController.queryable_with :offset, :scope => lambda {|offset| { :offset => offset }}
171
+ get :index, :offset => 4, :limit => 2
172
+ assigns(:resource_full_mock_users).should == @users[4..5]
173
+ end
174
+
175
+ xit "doesn't attempt to paginate if pagination is disabled" do
176
+ ResourceFullMockUsersController.queryable_with :limit, :scope => lambda {|limit| { :limit => limit }}
177
+ ResourceFullMockUsersController.queryable_with :offset, :scope => lambda {|offset| { :offset => offset }}
178
+ ResourceFullMockUsersController.paginatable = false
179
+ get :index, :offset => 4, :limit => 2
180
+ assigns(:resource_full_mock_users).should == @users
181
+ ResourceFullMockUsersController.paginatable = true # cleanup
182
+ end
183
+ end
184
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --backtrace
@@ -0,0 +1,134 @@
1
+ # This file is copied to ~/spec when you run 'ruby script/generate rspec'
2
+ # from the project root directory.
3
+ ENV["RAILS_ENV"] = "test"
4
+
5
+ require File.expand_path(File.dirname(__FILE__) + '/../../../../config/environment')
6
+ require 'spec'
7
+ require 'spec/rails'
8
+ require 'mocha'
9
+ # require File.dirname(__FILE__) + "/../lib/resource_full"
10
+
11
+ Spec::Runner.configure do |config|
12
+ # If you're not using ActiveRecord you should remove these
13
+ # lines, delete config/database.yml and disable :active_record
14
+ # in your config/boot.rb
15
+ config.use_transactional_fixtures = true
16
+ config.use_instantiated_fixtures = false
17
+ config.fixture_path = RAILS_ROOT + '/vendor/plugins/resource_full/spec/fixtures/'
18
+ config.mock_with :mocha
19
+
20
+ config.before(:all) do
21
+ ActiveRecord::Base.connection.create_table "resource_full_mock_addresses", :force => true do |t|
22
+ t.string "street"
23
+ t.string "city"
24
+ t.string "state_code"
25
+ t.integer "zip"
26
+ t.integer "resource_full_mock_user_id"
27
+ t.timestamps
28
+ end
29
+
30
+ ActiveRecord::Base.connection.create_table "resource_full_mock_users", :force => true do |t|
31
+ t.string "first_name"
32
+ t.string "last_name"
33
+ t.date "birthdate"
34
+ t.string "email"
35
+ t.string "join_date"
36
+ t.integer "income"
37
+ t.integer "resource_full_mock_employer_id"
38
+ t.string "type"
39
+ t.timestamps
40
+ end
41
+
42
+ ActiveRecord::Base.connection.create_table "resource_full_mock_employers", :force => true do |t|
43
+ t.string "name"
44
+ t.string "email"
45
+ t.timestamps
46
+ end
47
+
48
+ ActiveRecord::Base.connection.create_table "resource_full_namespaced_mock_records", :force => true do |t|
49
+ t.string "name"
50
+ t.timestamps
51
+ end
52
+ end
53
+ end
54
+
55
+ ActionController::Routing::Routes.draw do |map|
56
+ map.foo '/foo', :controller => 'resource_full_mocks', :action => 'foo'
57
+ map.resources :resource_full_mocks, :collection => {:count => :get}
58
+ map.resources :resource_full_sub_mocks, :resource_full_mock_addresses
59
+ map.resources :resource_full_mock_users, :collection => {:count => :get} do |users|
60
+ users.resources :resource_full_mock_addresses
61
+ end
62
+ map.resources :resources, :controller => 'resource_full/controllers/resources' do |resource|
63
+ resource.resources :routes, :controller => 'resource_full/controllers/routes'
64
+ end
65
+ map.resources :resource_full_namespaced_mock_records
66
+ map.resources :resource_full_namespaced_mock_record_with_xml_overrides
67
+ end
68
+
69
+ class ResourceFullMock
70
+ def self.table_name; "mock"; end
71
+ end
72
+ class ResourceFullSubMock < ResourceFullMock; end
73
+
74
+ # TODO Remove these or find a better way to handle ActiveRecord dependencies.
75
+ class ResourceFullMockEmployer < ActiveRecord::Base
76
+ has_many :resource_full_mock_users
77
+ end
78
+
79
+ class ResourceFullMockUser < ActiveRecord::Base
80
+ belongs_to :resource_full_mock_employer
81
+ has_many :resource_full_mock_addresses
82
+ end
83
+
84
+ class ResourceFullMockSubUser < ResourceFullMockUser
85
+ end
86
+
87
+ class ResourceFullMockAddress < ActiveRecord::Base
88
+ belongs_to :resource_full_mock_user
89
+ end
90
+
91
+ class ResourceFullMocksController < ResourceFull::Base
92
+ # dispatch_spec custom methods spec, approx. line 98
93
+ def foo
94
+ render :xml => { :foo => "bar" }.to_xml
95
+ end
96
+ end
97
+ class ResourceFullSubMocksController < ResourceFullMocksController; end
98
+ class ResourceFullMockUsersController < ResourceFull::Base; end
99
+ class ResourceFullMockAddressesController < ResourceFull::Base; end
100
+
101
+ module ResourceFullSpec
102
+ class ResourceFullNamespacedMockRecord < ActiveRecord::Base
103
+ end
104
+ end
105
+
106
+ class ResourceFullNamespacedMockRecordsController < ResourceFull::Base
107
+ exposes ResourceFullSpec::ResourceFullNamespacedMockRecord
108
+ end
109
+
110
+ class ResourceFullNamespacedMockRecordWithXmlOverridesController < ResourceFull::Base
111
+ exposes ResourceFullSpec::ResourceFullNamespacedMockRecord
112
+ def show_xml_options; {:root => 'my_show_root'}; end
113
+ def index_xml_options; {:root => 'my_index_roots'}; end
114
+ def create_xml_options; {:root => 'my_create_root'}; end
115
+ def update_xml_options; {:root => 'my_update_root'}; end
116
+ def new_xml_options; {:root => 'my_new_root'}; end
117
+ end
118
+
119
+ ActionController::Routing.use_controllers! %w{
120
+ resource_full_mock_users
121
+ resource_full_mock_addresses
122
+ resource_full_mocks
123
+ resource_full_sub_mocks
124
+ resource_full/controllers/routes
125
+ resource_full/controllers/resources
126
+ }
127
+
128
+ def putsh(stuff)
129
+ puts "#{ERB::Util.h(stuff)}<br/>"
130
+ end
131
+
132
+ def ph(stuff)
133
+ putsh stuff.inspect
134
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resource_full
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 7
8
+ - 6
9
+ version: 0.7.6
10
+ platform: ruby
11
+ authors:
12
+ - Brian Guthrie
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-16 00:00:00 +05:30
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: action_controller
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 1
30
+ - 0
31
+ version: 2.1.0
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: active_record
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 1
44
+ - 0
45
+ version: 2.1.0
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id003
60
+ - !ruby/object:Gem::Dependency
61
+ name: mocha
62
+ prerelease: false
63
+ requirement: &id004 !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ type: :development
71
+ version_requirements: *id004
72
+ description:
73
+ email: btguthrie@gmail.com
74
+ executables: []
75
+
76
+ extensions: []
77
+
78
+ extra_rdoc_files: []
79
+
80
+ files:
81
+ - lib/resource_full/base.rb
82
+ - lib/resource_full/controllers/resources.rb
83
+ - lib/resource_full/controllers/resources_controller.rb
84
+ - lib/resource_full/controllers/routes_controller.rb
85
+ - lib/resource_full/core_extensions/api.rb
86
+ - lib/resource_full/core_extensions/exception.rb
87
+ - lib/resource_full/core_extensions/from_json.rb
88
+ - lib/resource_full/core_extensions/module.rb
89
+ - lib/resource_full/dispatch.rb
90
+ - lib/resource_full/models/resourced_route.rb
91
+ - lib/resource_full/query.rb
92
+ - lib/resource_full/render/html.rb
93
+ - lib/resource_full/render/json.rb
94
+ - lib/resource_full/render/xml.rb
95
+ - lib/resource_full/render.rb
96
+ - lib/resource_full/retrieve.rb
97
+ - lib/resource_full/version.rb
98
+ - lib/resource_full.rb
99
+ - MIT-LICENSE
100
+ - Rakefile
101
+ - README.rdoc
102
+ - spec/resource_full/base_spec.rb
103
+ - spec/resource_full/controllers/resources_spec.rb
104
+ - spec/resource_full/dispatch_spec.rb
105
+ - spec/resource_full/models/resourced_route_spec.rb
106
+ - spec/resource_full/query/parameter_spec.rb
107
+ - spec/resource_full/query_spec.rb
108
+ - spec/resource_full/render/html_spec.rb
109
+ - spec/resource_full/render/json_spec.rb
110
+ - spec/resource_full/render/xml_spec.rb
111
+ - spec/resource_full/render_spec.rb
112
+ - spec/resource_full/retrieve_spec.rb
113
+ - spec/spec.opts
114
+ - spec/spec_helper.rb
115
+ has_rdoc: true
116
+ homepage: http://github.com/bguthrie/resource_full
117
+ licenses: []
118
+
119
+ post_install_message:
120
+ rdoc_options: []
121
+
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ segments:
129
+ - 0
130
+ version: "0"
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ segments:
136
+ - 0
137
+ version: "0"
138
+ requirements: []
139
+
140
+ rubyforge_project:
141
+ rubygems_version: 1.3.6
142
+ signing_key:
143
+ specification_version: 3
144
+ summary: A library for building controllers that correctly interact with ActiveResource.
145
+ test_files:
146
+ - spec/resource_full/base_spec.rb
147
+ - spec/resource_full/controllers/resources_spec.rb
148
+ - spec/resource_full/dispatch_spec.rb
149
+ - spec/resource_full/models/resourced_route_spec.rb
150
+ - spec/resource_full/query/parameter_spec.rb
151
+ - spec/resource_full/query_spec.rb
152
+ - spec/resource_full/render/html_spec.rb
153
+ - spec/resource_full/render/json_spec.rb
154
+ - spec/resource_full/render/xml_spec.rb
155
+ - spec/resource_full/render_spec.rb
156
+ - spec/resource_full/retrieve_spec.rb