Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: jamesgolick/resource_controller
...
head fork: strikeroff/resource_controller
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 5 commits
  • 8 files changed
  • 0 commit comments
  • 2 contributors
View
4 .gitignore
@@ -7,3 +7,7 @@ nbproject/*
rdoc
test/db/test.sqlite3
.specification
+*.bat
+.idea
+*.gem
+.idea
View
31 README.rdoc
@@ -6,12 +6,27 @@ resource_controller makes RESTful controllers easier, more maintainable, and sup
Install it as a plugin:
- script/plugin install git://github.com/giraffesoft/resource_controller.git
+ script/plugin install git://github.com/strikeroff/resource_controller.git
+
+Or install it as a gem in your enviroment.rb:
+
+ config.gem "strikeroff-resource_controller",:lib => 'resource_controller', :source => 'http://gems.github.com'
+
+ it gives the source
+ and include it as plugin
+
+ config.plugins = [ :resource_controller]
+
+ run
+
+ rake gems:install
Or grab the source
- git clone git://github.com/giraffesoft/resource_controller.git
+ git clone git://github.com/strikeroff/resource_controller.git
+
+
= Usage
Creating a basic RESTful controller is as easy as...
@@ -321,6 +336,18 @@ Or with namespaced, nested controllers...
You get the idea. Everything is automagical! All parameters are inferred.
+Different from original
+
+ 1) Changing actions wrap with transaction
+
+ 2) Every resource has 3 additional routes:
+ show_attribute - show one attribute
+ update_attribute - update one attribute
+ update_attributes - deprecated for now
+
+ see rake routes for urls and params
+
+
== Credits
resource_controller was created, and is maintained by {James Golick}[http://jamesgolick.com].
View
62 lib/resource_controller.rb
@@ -1,26 +1,78 @@
+require File.join(File.dirname(__FILE__), "urligence.rb") unless ::Object.const_defined? "Urligence"
+
begin
require_dependency 'application_controller'
rescue LoadError => e
require_dependency 'application'
end
+
module ResourceController
ACTIONS = [:index, :show, :new_action, :create, :edit, :update, :destroy].freeze
SINGLETON_ACTIONS = (ACTIONS - [:index]).freeze
FAILABLE_ACTIONS = ACTIONS - [:index, :new_action, :edit].freeze
- NAME_ACCESSORS = [:model_name, :route_name, :object_name]
-
+ NAME_ACCESSORS = [:model_name, :route_name, :object_name]
+
module ActionControllerExtension
unloadable
-
+
def resource_controller(*args)
include ResourceController::Controller
-
+
if args.include?(:singleton)
include ResourceController::Helpers::SingletonCustomizations
end
- end
+ end
end
end
+#
+#"Urligence"
+#"ResourceController"
+#"Base"
+#"Controller "
+#"Helpers"
+#"Urls"
+#"Internal"
+#"Nested"
+#"CurrentObjects"
+#"Action"
+#"Accessors"
+#"ClassMethods"
+#"ActionOptions"
+#"ResponseCollector"
+#"FailableActionOptions"
+#"Singleton"
+#"SingletonCustomizations"
+
+require File.join(File.dirname(__FILE__), "resource_controller", "base.rb")
+
+require File.join(File.dirname(__FILE__), "resource_controller", "controller.rb")
+
+
+require File.join(File.dirname(__FILE__), "resource_controller", "helpers.rb")
+#***************************Helpers******************************#
+require File.join(File.dirname(__FILE__), "resource_controller", "helpers", "urls.rb")
+require File.join(File.dirname(__FILE__), "resource_controller", "helpers", "internal.rb")
+require File.join(File.dirname(__FILE__), "resource_controller", "helpers", "nested.rb")
+require File.join(File.dirname(__FILE__), "resource_controller", "helpers", "current_objects.rb")
+#**************************************************************#
+require File.join(File.dirname(__FILE__), "resource_controller", "actions.rb")
+require File.join(File.dirname(__FILE__), "resource_controller", "accessors.rb")
+require File.join(File.dirname(__FILE__), "resource_controller", "class_methods.rb")
+require File.join(File.dirname(__FILE__), "resource_controller", "action_options.rb")
+require File.join(File.dirname(__FILE__), "resource_controller", "response_collector.rb")
+require File.join(File.dirname(__FILE__), "resource_controller", "failable_action_options.rb")
+#*******************************Singleton**********************************#
+
+require File.join(File.dirname(__FILE__), "resource_controller", "singleton.rb")
+require File.join(File.dirname(__FILE__), "resource_controller", "helpers", "singleton_customizations.rb")
+
+require File.join(File.dirname(__FILE__), "resource_controller", "routing_ext.rb")
+
+ActionController::Resources.send :include, ResourceController::RoutingExt
+
require File.dirname(__FILE__)+'/../rails/init.rb' unless ActionController::Base.include?(Urligence)
+
+
+
View
111 lib/resource_controller/actions.rb
@@ -1,12 +1,12 @@
module ResourceController
module Actions
-
+
def index
load_collection
before :index
response_for :index
end
-
+
def show
load_object
before :show
@@ -16,31 +16,35 @@ def show
end
def create
- build_object
- load_object
- before :create
- if object.save
- after :create
- set_flash :create
- response_for :create
- else
- after :create_fails
- set_flash :create_fails
- response_for :create_fails
+ end_of_association_chain.transaction do
+ build_object
+ load_object
+ before :create
+ if object.save
+ after :create
+ set_flash :create
+ response_for :create
+ else
+ after :create_fails
+ set_flash :create_fails
+ response_for :create_fails
+ end
end
end
def update
- load_object
- before :update
- if object.update_attributes object_params
- after :update
- set_flash :update
- response_for :update
- else
- after :update_fails
- set_flash :update_fails
- response_for :update_fails
+ end_of_association_chain.transaction do
+ load_object
+ before :update
+ if object.update_attributes object_params
+ after :update
+ set_flash :update
+ response_for :update
+ else
+ after :update_fails
+ set_flash :update_fails
+ response_for :update_fails
+ end
end
end
@@ -58,18 +62,57 @@ def edit
end
def destroy
- load_object
- before :destroy
- if object.destroy
- after :destroy
- set_flash :destroy
- response_for :destroy
- else
- after :destroy_fails
- set_flash :destroy_fails
- response_for :destroy_fails
+ end_of_association_chain.transaction do
+ load_object
+ before :destroy
+ if object.destroy
+ after :destroy
+ set_flash :destroy
+ response_for :destroy
+ else
+ after :destroy_fails
+ set_flash :destroy_fails
+ response_for :destroy_fails
+ end
+ end
+ end
+
+ def update_attribute
+ end_of_association_chain.transaction do
+ load_object
+ to_show = nil
+ value = params[:value]
+
+ #value = true if value.match(/true/i)
+ #value = false if value.match(/false/i)
+ object.update_attributes({params[:attribute] => value})
+ if match = params[:attribute].match(/[a-z]+(?=(_id))/i)
+ assoc_object = match[0].classify.constantize.find(params[:value])
+ to_show = assoc_object.name
+ end
+ render :text=> to_show|| params[:value]
end
end
-
+
+ def update_attributes
+ end_of_association_chain.transaction do
+ load_object
+ result = "error"
+ if !params[:value].blank? && params[:value].json?
+ result = "OK" if object.update_attributes(ActiveSupport::JSON.decode(params[:value]))
+ end
+ render :text=>result
+ end
+ end
+
+
+ def show_attribute
+ @object = object
+ @object = @object.send params[:attribute]
+ response_for :show
+ rescue ActiveRecord::RecordNotFound
+ response_for :show_fails
+ end
+
end
end
View
130 lib/resource_controller/helpers/current_objects.rb
@@ -2,72 +2,72 @@ module ResourceController
module Helpers
module CurrentObjects
protected
- # Used internally to return the model for your resource.
- #
- def model
- model_name.to_s.camelize.constantize
- end
+ # Used internally to return the model for your resource.
+ #
+ def model
+ model_name.to_s.camelize.constantize
+ end
-
- # Used to fetch the collection for the index method
- #
- # In order to customize the way the collection is fetched, to add something like pagination, for example, override this method.
- #
- def collection
- end_of_association_chain.find(:all)
- end
-
- # Returns the current param.
- #
- # Defaults to params[:id].
- #
- # Override this method if you'd like to use an alternate param name.
- #
- def param
- params[:id]
- end
-
- # Used to fetch the current member object in all of the singular methods that operate on an existing member.
- #
- # Override this method if you'd like to fetch your objects in some alternate way, like using a permalink.
- #
- # class PostsController < ResourceController::Base
- # private
- # def object
- # @object ||= end_of_association_chain.find_by_permalink(param)
- # end
- # end
- #
- def object
- @object ||= end_of_association_chain.find(param) unless param.nil?
- @object
- end
-
- # Used internally to load the member object in to an instance variable @#{model_name} (i.e. @post)
- #
- def load_object
- instance_variable_set "@#{parent_type}", parent_object if parent?
- instance_variable_set "@#{object_name}", object
- end
-
- # Used internally to load the collection in to an instance variable @#{model_name.pluralize} (i.e. @posts)
- #
- def load_collection
- instance_variable_set "@#{parent_type}", parent_object if parent?
- instance_variable_set "@#{object_name.to_s.pluralize}", collection
- end
-
- # Returns the form params. Defaults to params[model_name] (i.e. params["post"])
- #
- def object_params
- params["#{object_name}"]
- end
-
- # Builds the object, but doesn't save it, during the new, and create action.
- #
- def build_object
- @object ||= end_of_association_chain.send parent? ? :build : :new, object_params
- end
+
+ # Used to fetch the collection for the index method
+ #
+ # In order to customize the way the collection is fetched, to add something like pagination, for example, override this method.
+ #
+ def collection
+ end_of_association_chain.find(:all)
+ end
+
+ # Returns the current param.
+ #
+ # Defaults to params[:id].
+ #
+ # Override this method if you'd like to use an alternate param name.
+ #
+ def param
+ params[:id]
+ end
+
+ # Used to fetch the current member object in all of the singular methods that operate on an existing member.
+ #
+ # Override this method if you'd like to fetch your objects in some alternate way, like using a permalink.
+ #
+ # class PostsController < ResourceController::Base
+ # private
+ # def object
+ # @object ||= end_of_association_chain.find_by_permalink(param)
+ # end
+ # end
+ #
+ def object
+ @object ||= end_of_association_chain.find(param) unless param.nil?
+ @object
+ end
+
+ # Used internally to load the member object in to an instance variable @#{model_name} (i.e. @post)
+ #
+ def load_object
+ instance_variable_set "@#{parent_type}", parent_object if parent?
+ instance_variable_set "@#{object_name}", object
+ end
+
+ # Used internally to load the collection in to an instance variable @#{model_name.pluralize} (i.e. @posts)
+ #
+ def load_collection
+ instance_variable_set "@#{parent_type}", parent_object if parent?
+ instance_variable_set "@#{object_name.to_s.pluralize}", collection
+ end
+
+ # Returns the form params. Defaults to params[model_name] (i.e. params["post"])
+ #
+ def object_params
+ params["#{object_name}"]
+ end
+
+ # Builds the object, but doesn't save it, during the new, and create action.
+ #
+ def build_object
+ @object ||= end_of_association_chain.send parent? ? :build : :new, object_params
+ end
end
end
end
View
114 lib/resource_controller/routing_ext.rb
@@ -0,0 +1,114 @@
+#module ActionController
+# module Resources
+
+
+module ResourceController
+ module RoutingExt
+ def self.included(base)
+ base.alias_method_chain :map_member_actions, :attribute_support
+
+ base.send :define_method, :resource_controller_action_options_for do |action, resource, method|
+ default_options = { :action => action.to_s }
+ case default_options[:action]
+ when "show_attribute";
+ default_options.merge(add_conditions_for(resource.conditions, method || :get)).merge(resource.requirements(true)).merge({ :attribute => /[A-Za-z_]+/ }) # old cool but hard/([A-Za-z]+[_]?)+(?=[-])/
+ when "update_attribute";
+ default_options.merge(add_conditions_for(resource.conditions, method || :put)).merge(resource.requirements(true)).merge({ :attribute => /[A-Za-z_]+/})
+ when "update_attributes";
+ default_options.merge(add_conditions_for(resource.conditions, method || :put)).merge(resource.requirements(true))
+ else
+ default_options.merge(add_conditions_for(resource.conditions, method)).merge(resource.requirements)
+ end
+ end
+ end
+ private
+
+ #def resource_controller_action_options_for(action, resource, method = nil)
+ #
+ #end
+
+ def map_member_actions_with_attribute_support(map, resource)
+ map_member_actions_without_attribute_support(map, resource)
+ route_path = "#{resource.shallow_name_prefix}#{resource.singular}"
+ #
+ show_attribute_action_options = resource_controller_action_options_for("show_attribute", resource, :get)
+ #
+ map.send(:"attribute_#{route_path}", "#{resource.member_path}/attribute/:attribute", show_attribute_action_options)
+ map.connect("#{resource.member_path}/attribute/:attribute.:format", show_attribute_action_options)
+
+ update_attribute_action_options = resource_controller_action_options_for("update_attribute", resource, :put)
+ map.connect("#{resource.member_path}/attribute/:attribute", update_attribute_action_options)
+ update_attribute_action_options = resource_controller_action_options_for("update_attributes", resource, :put)
+ map.connect("#{resource.member_path}/attributes", update_attribute_action_options)
+ end
+
+
+ end
+end
+
+#
+#
+#["show_attribute",
+# <ActionController::Resources::Resource:0x4a51820
+#@nesting_path_prefix="/press_posts/:press_post_id",
+# @member_methods={:get=>[:edit]},
+# @conditions={},
+# @shallow_path_prefix=nil,
+# @name_prefix=nil,
+# @path_segment=:press_posts,
+# @plural=:press_posts,
+# @controller="press_posts",
+# @requirements={},
+# @path="/press_posts",
+# @path_prefix=nil,
+# @singular="press_post",
+# @id_requirement={:id=>/[^\/.?]+/},
+# @shallow_name_prefix=nil,
+# @new_methods={:get=>[:new]},
+# @member_path="/press_posts/:id",
+# @collection_methods={},
+# @action_separator="/",
+# @new_path="/press_posts/new",
+# @options={}, @allowed_actions={}>,
+# :get]
+
+
+# end
+#end
+
+
+
+
+#module ActionController
+# module Resources
+# alias_method :old_map_member_actions, :map_member_actions
+# private
+#
+# def map_member_actions(map, resource)
+# old_map_member_actions(map, resource)
+# route_path = "#{resource.shallow_name_prefix}#{resource.singular}"
+# show_attribute_action_options = resource_controller_action_options_for("show_attribute", resource, :get)
+# map.send(:"attribute_#{route_path}","#{resource.member_path}/attribute/:attribute", show_attribute_action_options)
+# map.connect("#{resource.member_path}/attribute/:attribute.:format", show_attribute_action_options)
+#
+# update_attribute_action_options = resource_controller_action_options_for("update_attribute", resource, :put)
+# map.connect("#{resource.member_path}/attribute/:attribute", update_attribute_action_options)
+# update_attribute_action_options = resource_controller_action_options_for("update_attributes", resource, :put)
+# map.connect("#{resource.member_path}/attributes", update_attribute_action_options)
+# end
+#
+# def resource_controller_action_options_for(action, resource, method = nil)
+# default_options = { :action => action.to_s }
+# case default_options[:action]
+# when "show_attribute";
+# default_options.merge(add_conditions_for(resource.conditions, method || :get)).merge(resource.requirements(true)).merge({ :attribute => /[A-Za-z_]+/ }) # old cool but hard/([A-Za-z]+[_]?)+(?=[-])/
+# when "update_attribute";
+# default_options.merge(add_conditions_for(resource.conditions, method || :put)).merge(resource.requirements(true)).merge({ :attribute => /[A-Za-z_]+/})
+# when "update_attributes";
+# default_options.merge(add_conditions_for(resource.conditions, method || :put)).merge(resource.requirements(true))
+# else
+# default_options.merge(add_conditions_for(resource.conditions, method)).merge(resource.requirements)
+# end
+# end
+# end
+#end
View
3  rails/init.rb
@@ -1,6 +1,5 @@
ActionController::Base.class_eval do
include Urligence
- helper_method :smart_url
-
+ helper_method :smart_url
extend ResourceController::ActionControllerExtension
end
View
2  resource_controller.gemspec
@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = %q{resource_controller}
- s.version = "0.6.5"
+ s.version = "0.6.6"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["James Golick"]

No commit comments for this range

Something went wrong with that request. Please try again.