giraffesoft-resource_controller 0.4.9 → 0.4.10

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 (50) hide show
  1. data/README +312 -1
  2. data/lib/resource_controller.rb +10 -5
  3. data/lib/resource_controller/class_methods.rb +3 -1
  4. data/lib/resource_controller/controller.rb +6 -0
  5. data/lib/resource_controller/helpers/nested.rb +21 -3
  6. data/lib/resource_controller/helpers/singleton_customizations.rb +60 -0
  7. data/lib/resource_controller/helpers/urls.rb +5 -1
  8. data/lib/resource_controller/singleton.rb +15 -0
  9. data/lib/resource_controller/version.rb +1 -1
  10. data/lib/tasks/gem.rake +1 -1
  11. data/test/app/controllers/accounts_controller.rb +6 -0
  12. data/test/app/controllers/cms/products_controller.rb +1 -1
  13. data/test/app/controllers/images_controller.rb +4 -0
  14. data/test/app/controllers/options_controller.rb +8 -0
  15. data/test/app/helpers/accounts_helper.rb +2 -0
  16. data/test/app/helpers/images_helper.rb +2 -0
  17. data/test/app/models/account.rb +1 -0
  18. data/test/app/models/image.rb +3 -0
  19. data/test/app/models/user.rb +3 -0
  20. data/test/app/views/accounts/_form.html.erb +4 -0
  21. data/test/app/views/accounts/edit.html.erb +14 -0
  22. data/test/app/views/accounts/new.html.erb +12 -0
  23. data/test/app/views/accounts/show.html.erb +5 -0
  24. data/test/app/views/images/_form.html.erb +4 -0
  25. data/test/app/views/images/edit.html.erb +14 -0
  26. data/test/app/views/images/new.html.erb +12 -0
  27. data/test/app/views/options/_form.html.erb +8 -0
  28. data/test/app/views/options/edit.html.erb +16 -0
  29. data/test/app/views/options/index.html.erb +21 -0
  30. data/test/app/views/options/new.html.erb +12 -0
  31. data/test/app/views/options/show.html.erb +10 -0
  32. data/test/config/database.yml +0 -3
  33. data/test/config/environment.rb +2 -2
  34. data/test/config/routes.rb +8 -1
  35. data/test/db/migrate/002_create_products.rb +1 -1
  36. data/test/db/migrate/004_create_options.rb +3 -2
  37. data/test/db/migrate/011_create_images.rb +12 -0
  38. data/test/db/migrate/012_create_users.rb +11 -0
  39. data/test/db/schema.rb +13 -6
  40. data/test/test/fixtures/images.yml +6 -0
  41. data/test/test/fixtures/users.yml +5 -0
  42. data/test/test/functional/images_controller_test.rb +37 -0
  43. data/test/test/unit/helpers/current_objects_test.rb +6 -0
  44. data/test/test/unit/helpers/nested_test.rb +5 -1
  45. data/test/test/unit/helpers/singleton_current_objects_test.rb +68 -0
  46. data/test/test/unit/helpers/singleton_nested_test.rb +77 -0
  47. data/test/test/unit/helpers/singleton_urls_test.rb +67 -0
  48. data/test/test/unit/helpers/urls_test.rb +5 -1
  49. data/test/test/unit/image_test.rb +7 -0
  50. metadata +35 -2
@@ -110,7 +110,11 @@ module ResourceController::Helpers::Urls
110
110
  end
111
111
 
112
112
  def parent_url_options
113
- parent? ? [parent_type.to_sym, parent_object] : nil
113
+ if parent?
114
+ parent_singleton? ? parent_type.to_sym : [parent_type.to_sym, parent_object]
115
+ else
116
+ nil
117
+ end
114
118
  end
115
119
 
116
120
  # Returns all of the current namespaces of the current controller, symbolized, in array form.
@@ -0,0 +1,15 @@
1
+ module ResourceController
2
+
3
+ # == ResourceController::Singleton
4
+ #
5
+ # Inherit from this class to create your RESTful singleton controller. See the README for usage.
6
+ #
7
+ class Singleton < ApplicationController
8
+ unloadable
9
+
10
+ def self.inherited(subclass)
11
+ super
12
+ subclass.class_eval { resource_controller :singleton }
13
+ end
14
+ end
15
+ end
@@ -2,7 +2,7 @@ module ResourceController
2
2
  module VERSION
3
3
  MAJOR = 0
4
4
  MINOR = 4
5
- TINY = 9
5
+ TINY = 10
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/lib/tasks/gem.rake CHANGED
@@ -43,7 +43,7 @@ task :gem => :tag_warn
43
43
 
44
44
  namespace :gem do
45
45
  desc 'Upload gems to rubyforge.org'
46
- task :release => :gem do
46
+ task :rubyforge => :gem do
47
47
  sh 'rubyforge login'
48
48
  sh "rubyforge add_release giraffesoft resource_controller #{ResourceController::VERSION::STRING} pkg/#{spec.full_name}.gem"
49
49
  sh "rubyforge add_file giraffesoft resource_controller #{ResourceController::VERSION::STRING} pkg/#{spec.full_name}.gem"
@@ -0,0 +1,6 @@
1
+ class AccountsController < ResourceController::Singleton
2
+ protected
3
+ def object
4
+ Account.find(:first)
5
+ end
6
+ end
@@ -1,3 +1,3 @@
1
1
  class Cms::ProductsController < ResourceController::Base
2
- create.flash_now 'something'
2
+ create.flash 'something'
3
3
  end
@@ -0,0 +1,4 @@
1
+ class ImagesController < ResourceController::Singleton
2
+ belongs_to :user
3
+ actions :create
4
+ end
@@ -0,0 +1,8 @@
1
+ class OptionsController < ResourceController::Base
2
+ belongs_to :account
3
+
4
+ protected
5
+ def parent_object
6
+ Account.find(:first)
7
+ end
8
+ end
@@ -0,0 +1,2 @@
1
+ module AccountsHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module ImagesHelper
2
+ end
@@ -1,3 +1,4 @@
1
1
  class Account < ActiveRecord::Base
2
2
  has_many :photos
3
+ has_many :options
3
4
  end
@@ -0,0 +1,3 @@
1
+ class Image < ActiveRecord::Base
2
+ belongs_to :user
3
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ has_one :image
3
+ end
@@ -0,0 +1,4 @@
1
+ <p>
2
+ <label for="account_name">Name:</label>
3
+ <%= f.text_field :name %>
4
+ </p>
@@ -0,0 +1,14 @@
1
+ <h1>Editing Account</h1>
2
+
3
+ <%= error_messages_for :account %>
4
+
5
+ <% form_for(:account, :url => object_url, :html => { :method => :put }) do |f| %>
6
+ <%= render :partial => "form", :locals => { :f => f } %>
7
+ <p>
8
+ <%=submit_tag "Update"%>
9
+ </p>
10
+ <% end %>
11
+
12
+ <br/>
13
+
14
+ <%= link_to 'Show', object_url %>
@@ -0,0 +1,12 @@
1
+ <h1>New Account</h1>
2
+
3
+ <%= error_messages_for :account %>
4
+
5
+ <% form_for(:account, :url => object_url) do |f| %>
6
+ <%= render :partial => "form", :locals => { :f => f } %>
7
+ <p>
8
+ <%= submit_tag "Create" %>
9
+ </p>
10
+ <% end %>
11
+ <br/>
12
+ <%= link_to 'Back', object_url %>
@@ -0,0 +1,5 @@
1
+ <p>
2
+ <strong>Name:</strong><%=h @account.name %>
3
+ </p>
4
+
5
+ <%= link_to 'Edit', edit_object_url %>
@@ -0,0 +1,4 @@
1
+ <p>
2
+ <label for="image_user_id">User:</label>
3
+ <%= f.text_field :user_id %>
4
+ </p>
@@ -0,0 +1,14 @@
1
+ <h1>Editing Image</h1>
2
+
3
+ <%= error_messages_for :image %>
4
+
5
+ <% form_for(:image, :url => object_url, :html => { :method => :put }) do |f| %>
6
+ <%= render :partial => "form", :locals => { :f => f } %>
7
+ <p>
8
+ <%=submit_tag "Update"%>
9
+ </p>
10
+ <% end %>
11
+
12
+ <br/>
13
+
14
+ <%= link_to 'Show', object_url %>
@@ -0,0 +1,12 @@
1
+ <h1>New Image</h1>
2
+
3
+ <%= error_messages_for :image %>
4
+
5
+ <% form_for(:image, :url => object_url) do |f| %>
6
+ <%= render :partial => "form", :locals => { :f => f } %>
7
+ <p>
8
+ <%= submit_tag "Create" %>
9
+ </p>
10
+ <% end %>
11
+ <br/>
12
+ <%= link_to 'Back', object_url %>
@@ -0,0 +1,8 @@
1
+ <p>
2
+ <label for="option_account_id">Account:</label>
3
+ <%= f.text_field :account_id %>
4
+ </p>
5
+ <p>
6
+ <label for="option_name">Title:</label>
7
+ <%= f.text_field :title %>
8
+ </p>
@@ -0,0 +1,16 @@
1
+ <h1>Editing Option</h1>
2
+
3
+ <%= error_messages_for :option %>
4
+
5
+ <% form_for(:option, :url => object_url, :html => { :method => :put }) do |f| %>
6
+ <%= render :partial => "form", :locals => { :f => f } %>
7
+ <p>
8
+ <%=submit_tag "Update"%>
9
+ </p>
10
+ <% end %>
11
+
12
+ <br/>
13
+
14
+ <%= link_to 'Show', object_url %>
15
+ |
16
+ <%= link_to 'Back', collection_url %>
@@ -0,0 +1,21 @@
1
+ <h1>Listing Options</h1>
2
+
3
+ <table>
4
+ <tr>
5
+ <th>Account</th>
6
+ <th>Title</th>
7
+ </tr>
8
+ <%- @options.each do |option|%>
9
+ <tr>
10
+ <td><%=h option.account_id %></td>
11
+ <td><%=h option.title %></td>
12
+
13
+ <td><%=link_to 'Show', object_url(option) %></td>
14
+ <td><%=link_to 'Edit', edit_object_url(option) %></td>
15
+ <td><%=link_to 'Destroy', object_url(option), :confirm => 'Are you sure?', :method => :delete %></td>
16
+ </tr>
17
+ <% end %>
18
+ </table>
19
+ <br/>
20
+
21
+ <%= link_to 'New Option', new_object_url %>
@@ -0,0 +1,12 @@
1
+ <h1>New Option</h1>
2
+
3
+ <%= error_messages_for :option %>
4
+
5
+ <% form_for(:option, :url => collection_url) do |f| %>
6
+ <%= render :partial => "form", :locals => { :f => f } %>
7
+ <p>
8
+ <%= submit_tag "Create" %>
9
+ </p>
10
+ <% end %>
11
+ <br/>
12
+ <%= link_to 'Back', collection_url %>
@@ -0,0 +1,10 @@
1
+ <p>
2
+ <strong>Account:</strong><%=h @option.account_id %>
3
+ </p>
4
+ <p>
5
+ <strong>Title:</strong><%=h @option.title %>
6
+ </p>
7
+
8
+ <%= link_to 'Edit', edit_object_url %>
9
+ |
10
+ <%= link_to 'Back', collection_url %>
@@ -5,9 +5,6 @@ development:
5
5
  password:
6
6
  socket: /tmp/mysql.sock
7
7
 
8
- # Warning: The database defined as 'test' will be erased and
9
- # re-generated from your development database when you run 'rake'.
10
- # Do not set this db to the same as development or production.
11
8
  test:
12
9
  adapter: mysql
13
10
  database: resource_controller_test
@@ -5,7 +5,7 @@
5
5
  # ENV['RAILS_ENV'] ||= 'production'
6
6
 
7
7
  # Specifies gem version of Rails to use when vendor/rails is not present
8
- RAILS_GEM_VERSION = '2.1' unless defined? RAILS_GEM_VERSION
8
+ RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION
9
9
 
10
10
  # Bootstrap the Rails environment, frameworks, and default configuration
11
11
  require File.join(File.dirname(__FILE__), 'boot')
@@ -43,7 +43,7 @@ Rails::Initializer.run do |config|
43
43
 
44
44
  # See Rails::Configuration for more options
45
45
 
46
- config.action_controller.session = { :session_key => "_myapp_session", :secret => "some secret phrase" }
46
+ config.action_controller.session = { :session_key => "_myapp_session", :secret => "6c1372e61789239a727cdbc8252eb6da" }
47
47
  end
48
48
 
49
49
  require "#{RAILS_ROOT}/../init"
@@ -7,6 +7,7 @@ ActionController::Routing::Routes.draw do |map|
7
7
 
8
8
  map.resources :users do |user|
9
9
  user.resources :photos, :name_prefix => "user_"
10
+ user.resource :image
10
11
  end
11
12
 
12
13
  map.resources :somethings
@@ -26,7 +27,13 @@ ActionController::Routing::Routes.draw do |map|
26
27
  end
27
28
 
28
29
  map.resources :comments
29
-
30
+
31
+ map.resource :account, :has_many => :options
32
+
33
+ map.resource :image
34
+
35
+ map.resources :options
36
+
30
37
  # The priority is based upon order of creation: first created -> highest priority.
31
38
 
32
39
  # Sample of regular route:
@@ -6,6 +6,6 @@ class CreateProducts < ActiveRecord::Migration
6
6
  end
7
7
 
8
8
  def self.down
9
- drop_table :cms_products
9
+ drop_table :products
10
10
  end
11
11
  end
@@ -1,8 +1,9 @@
1
1
  class CreateOptions < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :options do |t|
4
- t.column :product_id, :integer
5
- t.column :title, :string
4
+ t.references :product
5
+ t.references :account
6
+ t.string :title
6
7
  end
7
8
  end
8
9
 
@@ -0,0 +1,12 @@
1
+ class CreateImages < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :images, :force => true do |t|
4
+ t.references :user
5
+ t.timestamps
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ drop_table :images
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :users, :force => true do |t|
4
+ t.timestamps
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :users
10
+ end
11
+ end
data/test/db/schema.rb CHANGED
@@ -9,7 +9,7 @@
9
9
  #
10
10
  # It's strongly recommended to check this file into your version control system.
11
11
 
12
- ActiveRecord::Schema.define(:version => 11) do
12
+ ActiveRecord::Schema.define(:version => 12) do
13
13
 
14
14
  create_table "accounts", :force => true do |t|
15
15
  t.string "name"
@@ -21,8 +21,15 @@ ActiveRecord::Schema.define(:version => 11) do
21
21
  t.text "body"
22
22
  end
23
23
 
24
+ create_table "images", :force => true do |t|
25
+ t.integer "user_id"
26
+ t.datetime "created_at"
27
+ t.datetime "updated_at"
28
+ end
29
+
24
30
  create_table "options", :force => true do |t|
25
31
  t.integer "product_id"
32
+ t.integer "account_id"
26
33
  t.string "title"
27
34
  end
28
35
 
@@ -49,11 +56,6 @@ ActiveRecord::Schema.define(:version => 11) do
49
56
  t.string "title"
50
57
  end
51
58
 
52
- create_table "ratings", :force => true do |t|
53
- t.integer "comment_id"
54
- t.integer "stars"
55
- end
56
-
57
59
  create_table "somethings", :force => true do |t|
58
60
  t.string "title"
59
61
  end
@@ -62,4 +64,9 @@ ActiveRecord::Schema.define(:version => 11) do
62
64
  t.string "name"
63
65
  end
64
66
 
67
+ create_table "users", :force => true do |t|
68
+ t.datetime "created_at"
69
+ t.datetime "updated_at"
70
+ end
71
+
65
72
  end
@@ -0,0 +1,6 @@
1
+ one:
2
+ id: 1
3
+ user_id: 1
4
+ two:
5
+ id: 2
6
+ user_id: 1
@@ -0,0 +1,5 @@
1
+ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
+ one:
3
+ id: 1
4
+ two:
5
+ id: 2
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require 'images_controller'
3
+
4
+ # Re-raise errors caught by the controller.
5
+ class ImagesController; def rescue_action(e) raise e end; end
6
+
7
+ class ImagesControllerTest < Test::Unit::TestCase
8
+ def setup
9
+ @controller = ImagesController.new
10
+ @request = ActionController::TestRequest.new
11
+ @response = ActionController::TestResponse.new
12
+ @image = images :one
13
+ end
14
+
15
+ context "with user as parent" do
16
+
17
+ context "on post to :create" do
18
+ setup do
19
+ post :create, :user_id => 1, :photo => {}
20
+ end
21
+
22
+ should_redirect_to 'user_image_path(@image.user)'
23
+ should_assign_to :image
24
+ should_assign_to :user
25
+ should "scope image to user" do
26
+ assert users(:one), assigns(:image).user
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ should "not respond to show" do
33
+ assert_raise(ActionController::UnknownAction) do
34
+ get :show
35
+ end
36
+ end
37
+ end