Permalink
Browse files

Consolidate rails and restful rails

  • Loading branch information...
mattyoho committed Oct 20, 2011
1 parent b5002e9 commit 0b4a84ed92656f98c41039efb7fb58fa3a16f1f3
View
@@ -31,7 +31,6 @@ Gem::Specification.new do |s|
"lib/basic_assumption/default_assumption/class_resolver.rb",
"lib/basic_assumption/default_assumption/owner_builder.rb",
"lib/basic_assumption/default_assumption/rails.rb",
- "lib/basic_assumption/default_assumption/restful_rails.rb",
"lib/basic_assumption/rails.rb",
"lib/basic_assumption/rspec.rb",
"lib/basic_assumption/version.rb",
@@ -49,8 +48,7 @@ Gem::Specification.new do |s|
"spec/basic_assumption/default_assumption_spec.rb",
"spec/basic_assumption/default_assumption/base_spec.rb",
"spec/basic_assumption/default_assumption/class_resolver_spec.rb",
- "spec/basic_assumption/default_assumption/rails_spec.rb",
- "spec/basic_assumption/default_assumption/restful_rails_spec.rb"
+ "spec/basic_assumption/default_assumption/rails_spec.rb"
]
s.specification_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -1,10 +1,10 @@
-Feature: Restful Rails Create Action Is Correct
+Feature: Rails Create Action Is Correct
Scenario: restful create
Given a file named "app/controllers/widgets_controller.rb" with:
"""
class WidgetsController < ApplicationController
- default_assumption :restful_rails
+ default_assumption :rails
assume :widget
def create
@@ -1,10 +1,10 @@
-Feature: Restful Rails Destroy Action Is Correct
+Feature: Rails Destroy Action Is Correct
Scenario: restful destroy
Given a file named "app/controllers/widgets_controller.rb" with:
"""
class WidgetsController < ApplicationController
- default_assumption :restful_rails
+ default_assumption :rails
assume :widget
assume :widgets
@@ -1,10 +1,10 @@
-Feature: Restful Rails Edit Action Is Correct
+Feature: Rails Edit Action Is Correct
Scenario: restful edit
Given a file named "app/controllers/widgets_controller.rb" with:
"""
class WidgetsController < ApplicationController
- default_assumption :restful_rails
+ default_assumption :rails
assume :widget
end
"""
@@ -1,10 +1,10 @@
-Feature: Restful Rails Index Action Is Correct
+Feature: Rails Index Action Is Correct
Background:
Given a file named "app/controllers/widgets_controller.rb" with:
"""
class WidgetsController < ApplicationController
- default_assumption :restful_rails
+ default_assumption :rails
assume :widgets
assume :widget
end
@@ -1,10 +1,10 @@
-Feature: Restful Rails New Action Is Correct
+Feature: Rails New Action Is Correct
Scenario: restful new
Given a file named "app/controllers/widgets_controller.rb" with:
"""
class WidgetsController < ApplicationController
- default_assumption :restful_rails
+ default_assumption :rails
assume :widget
end
"""
@@ -1,4 +1,4 @@
-Feature: Restful Rails Scopes Find To Owner
+Feature: Rails Scopes Find To Owner
Background:
Given a file named "app/controllers/authenticating_controller.rb" with:
@@ -16,7 +16,7 @@ Feature: Restful Rails Scopes Find To Owner
And a file named "app/controllers/widgets_controller.rb" with:
"""
class WidgetsController < AuthenticatingController
- default_assumption :restful_rails
+ default_assumption :rails
assume :widget, :owner => :current_user, :raise_error => true
rescue_from ActiveRecord::RecordNotFound do
@@ -69,7 +69,7 @@ Feature: Restful Rails Scopes Find To Owner
end
class WidgetsController < AuthenticatingController
- default_assumption :restful_rails
+ default_assumption :rails
assume :widget, :owner => {:object => :current_owner, :column_name => :user_id}
def current_owner
@@ -1,4 +1,4 @@
-Feature: Restful Rails New Action Scoped To Owner
+Feature: Rails New Action Scoped To Owner
Scenario: Restful new with scoping
Given a file named "app/controllers/authenticating_controller.rb" with:
@@ -16,7 +16,7 @@ Feature: Restful Rails New Action Scoped To Owner
And a file named "app/controllers/widgets_controller.rb" with:
"""
class WidgetsController < AuthenticatingController
- default_assumption :restful_rails
+ default_assumption :rails
assume :widget, :owner => :current_user
end
"""
@@ -1,10 +1,10 @@
-Feature: Restful Rails Show Action Is Correct
+Feature: Rails Show Action Is Correct
Scenario: restful show
Given a file named "app/controllers/widgets_controller.rb" with:
"""
class WidgetsController < ApplicationController
- default_assumption :restful_rails
+ default_assumption :rails
assume :widget
end
"""
@@ -1,10 +1,10 @@
-Feature: Restful Rails Update Action Is Correct
+Feature: Rails Update Action Is Correct
Scenario: restful update
Given a file named "app/controllers/widgets_controller.rb" with:
"""
class WidgetsController < ApplicationController
- default_assumption :restful_rails
+ default_assumption :rails
assume :widget
def update
@@ -1,18 +1,56 @@
+require 'basic_assumption/default_assumption/owner_builder'
+
module BasicAssumption
module DefaultAssumption
- # Custom default behavior in the context of Rails.
- class Rails < BasicAssumption::DefaultAssumption::Base
- attr_reader :name, :context, :params, :request #:nodoc:
+ # Restful default behavior in the context of Rails
+ class Rails
+ attr_reader :action, :context,
+ :params, :name,
+ :request, :resource_attributes #:nodoc:
def initialize(name=nil, context={}, request=nil) #:nodoc:
- @name = name.to_s
- @context = context
- @request = request
- @params = request.params if request
+ @name = name.to_s
+ @context = context
+ @request = request
+ @params = request ? request.params : {}
+ @action = params['action']
+ @resource_attributes = params[singular_name] || {}
end
- # Returns a block that will attempt to find an instance of
- # an ActiveRecord model based on the name that was given to
- # BasicAssumption#assume and an id value in the parameters.
+
+ # Returns a block that will attempt to do the correct thing depending
+ # on the plurality of the name passed to +assume+ and the action for the
+ # current request. If the name is singular and the action is not 'new'
+ # or 'create', then +assume+ will find an instance of
+ # an ActiveRecord model of the name that it received and an id
+ # value in the parameters. If the action is 'new' or 'create', +assume+
+ # will instantiate a new instance of the model class, passing in the
+ # values it finds in the +params+ hash with for a key of the name passed
+ # to +assume+. For example:
+ #
+ # class WidgetController < ApplicationController
+ # default_assumption :rails
+ # assume :widget
+ #
+ # def create
+ # widget.save! # widget is: Widget.new(params[:widget])
+ # end
+ # end
+ #
+ # Note the object will have been instantiated but not saved, destroyed,
+ # etc.
+ #
+ # If the name passed to assume is plural, +assume+ returns all records
+ # for the model.
+ #
+ # It is possible to specify an alternative model name:
+ #
+ # class WidgetController < ApplicationController
+ # assume :sprocket, :as => :widget
+ # end
+ #
+ # This will create a +sprocket+ method in your actions and view
+ # that will use the Widget model for its lookup.
+ #
# The following two examples would be equivalent:
#
# class WidgetController < ActionController::Base
@@ -49,6 +87,7 @@ def initialize(name=nil, context={}, request=nil) #:nodoc:
# configuration options, such as:
#
# conf.active_record.raise_error = true
+ # conf.active_record.find_on_id = true
#
# It is possible to specify an alternative model name:
#
@@ -67,23 +106,52 @@ def block
end
def result #:nodoc:
- begin
- model_class.find(lookup_id)
- rescue
- raise if settings[:raise_error]
- nil
+ if list?
+ list
+ elsif make?
+ model_class.new(resource_attributes.merge(owner_attributes))
+ elsif lookup?
+ begin
+ record = model_class.where(owner_attributes).find(lookup_id)
+ record.attributes = resource_attributes unless request.get?
+ record
+ rescue
+ raise if settings[:raise_error]
+ nil
+ end
end
end
protected
- def lookup_id #:nodoc:
- if settings[:find_on_id]
- params["#{name}_id"] || params['id']
+
+ def owner_attributes
+ @owner_attributes ||= if context[:owner]
+ OwnerBuilder.new(context[:owner], context[:controller]).attributes
else
- params["#{name}_id"]
+ {}
end
end
+ def list #:nodoc:
+ model_class.all
+ end
+
+ def list? #:nodoc:
+ plural_name.eql?(model_name)
+ end
+
+ def lookup_id #:nodoc:
+ params['id']
+ end
+
+ def lookup? #:nodoc:
+ lookup_id && !list?
+ end
+
+ def make? #:nodoc:
+ %w(new create).include?(action) || !(lookup? || list?)
+ end
+
def model_class #:nodoc:
@model_class ||= model_name.classify.constantize
end
@@ -96,6 +164,13 @@ def settings #:nodoc:
@global_settings ||= BasicAssumption::Configuration.settings
@global_settings.merge(context)
end
+ def plural_name #:nodoc:
+ model_name.pluralize
+ end
+
+ def singular_name #:nodoc:
+ model_name.singularize
+ end
end
end
end
Oops, something went wrong.

0 comments on commit 0b4a84e

Please sign in to comment.