Permalink
Browse files

Add responders gem

  • Loading branch information...
1 parent bd7c646 commit 60003b01706d45b348739fab5b6800192bee67e0 @joshuaclayton committed Jan 18, 2010
@@ -0,0 +1,71 @@
+--- !ruby/object:Gem::Specification
+name: responders
+version: !ruby/object:Gem::Version
+ version: 0.4.2
+platform: ruby
+authors:
+- "Jos\xC3\xA9 Valim"
+autorequire:
+bindir: bin
+cert_chain: []
+
+date: 2010-01-05 00:00:00 -05:00
+default_executable:
+dependencies: []
+
+description: A set of Rails 3 responders to dry up your application
+email: contact@plataformatec.com.br
+executables: []
+
+extensions: []
+
+extra_rdoc_files:
+- README.rdoc
+files:
+- CHANGELOG.rdoc
+- MIT-LICENSE
+- README.rdoc
+- Rakefile
+- lib/generators/USAGE
+- lib/generators/responders_controller_generator.rb
+- lib/generators/responders_install_generator.rb
+- lib/generators/templates/controller.rb
+- lib/responders.rb
+- lib/responders/flash_responder.rb
+- lib/responders/http_cache_responder.rb
+- lib/responders/version.rb
+- test/flash_responder_test.rb
+- test/http_cache_responder_test.rb
+- test/test_helper.rb
+has_rdoc: true
+homepage: http://github.com/plataformatec/responders
+licenses: []
+
+post_install_message:
+rdoc_options:
+- --charset=UTF-8
+require_paths:
+- lib
+required_ruby_version: !ruby/object:Gem::Requirement
+ requirements:
+ - - ">="
+ - !ruby/object:Gem::Version
+ version: "0"
+ version:
+required_rubygems_version: !ruby/object:Gem::Requirement
+ requirements:
+ - - ">="
+ - !ruby/object:Gem::Version
+ version: "0"
+ version:
+requirements: []
+
+rubyforge_project:
+rubygems_version: 1.3.5
+signing_key:
+specification_version: 3
+summary: A set of Rails 3 responders to dry up your application
+test_files:
+- test/flash_responder_test.rb
+- test/http_cache_responder_test.rb
+- test/test_helper.rb
@@ -0,0 +1,10 @@
+== 0.4
+
+* Added Responders::FlashResponder.flash_keys and default to [ :notice, :alert ]
+* Added support to respond_with(@resource, :notice => "Yes!", :alert => "No!")
+
+== 0.1
+
+* Added FlashResponder
+* Added HttpCacheResponder
+* Added responders generators
@@ -0,0 +1,20 @@
+Copyright 2009 Plataforma Tecnologia. http://blog.plataformatec.com.br
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,66 @@
+== Responders
+
+A set of responders modules to dry up your Rails 3 app:
+
+* FlashResponder - Sets the flash based on the controller action and resource status.
+ For instance, if you do: respond_with(@post) on a POST request and the resource @post
+ does not contain errors, it will automatically set the flash message to
+ "Post was successfully created" as long as you configure your I18n file:
+
+ flash:
+ actions:
+ create:
+ notice: "{resource_name} was successfully created"
+ update:
+ notice: "{resource_name} was successfully updated"
+ destroy:
+ alert: "{resource_name} could not be destroyed"
+
+ In case the resource contains errors, you should use the failure key on I18n. This is
+ useful to dry up flash messages from your controllers. If you need a specific message
+ for a controller, let's say, for PostsController, you can also do:
+
+ flash:
+ posts:
+ create:
+ notice: "Your post was created and will be published soon"
+
+ This responder is activated in all non get requests. By default it will use the keys
+ :notice and :alert, but they can be changed as well:
+
+ Responders::FlashResponder.flash_keys = [ :success, :failure ]
+
+* HttpCacheResponder - Automatically adds Last-Modified headers to API requests. This
+ allows clients to easily query the server if a resource changed and if the client tries
+ to retrieve a resource that has not been modified, it returns not_modified status.
+
+== Configuring your own responder
+
+The first step is instal responders gem and configure it in your application:
+
+ sudo gem install responders
+
+Responders only provides a set of modules, to use them, you have to create your own
+responder. This can be done in an initializer for example:
+
+ class AppResponder < ActionController::Responder
+ include Responders::FlashResponder
+ include Responders::HttpCacheResponder
+ end
+
+== Generator
+
+This gem also includes a responders controller generator, so your scaffold can be customized
+to use respond_with instead of default respond_to blocks just by configuring your environment:
+
+ config.generators do |g|
+ g.scaffold_controller = :responders_controller
+ end
+
+== Bugs and Feedback
+
+If you discover any bugs or want to drop a line, feel free to create an issue on GitHub.
+
+http://github.com/plataformatec/responders/issues
+
+MIT License. Copyright 2009 Plataforma Tecnologia. http://blog.plataformatec.com.br
@@ -0,0 +1,44 @@
+# encoding: UTF-8
+
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+require File.join(File.dirname(__FILE__), 'lib', 'responders', 'version')
+
+desc 'Default: run unit tests'
+task :default => :test
+
+desc 'Test Responders'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for Responders'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Responders'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |s|
+ s.name = "responders"
+ s.version = Responders::VERSION
+ s.summary = "A set of Rails 3 responders to dry up your application"
+ s.email = "contact@plataformatec.com.br"
+ s.homepage = "http://github.com/plataformatec/responders"
+ s.description = "A set of Rails 3 responders to dry up your application"
+ s.authors = ['José Valim']
+ s.files = FileList["[A-Z]*", "lib/**/*", "init.rb"]
+ end
+
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
+end
@@ -0,0 +1,11 @@
+Description:
+ Stubs out a scaffolded controller and its views. Different from rails
+ scaffold_controller, it uses respond_with instead of respond_to blocks.
+ Pass the model name, either CamelCased or under_scored. The controller
+ name is retrieved as a pluralized version of the model name.
+
+ To create a controller within a module, specify the model name as a
+ path like 'parent_module/controller_name'.
+
+ This generates a controller class in app/controllers and invokes helper,
+ template engine and test framework generators.
@@ -0,0 +1,15 @@
+require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
+
+module Rails
+ module Generators
+ class RespondersControllerGenerator < ScaffoldControllerGenerator
+ def self.source_root
+ @source_root ||= File.expand_path("templates", File.dirname(__FILE__))
+ end
+ protected
+ def flash?
+ !ApplicationController.responder.ancestors.include?(Responders::FlashResponder)
+ end
+ end
+ end
+end
@@ -0,0 +1,14 @@
+class RespondersInstallGenerator < Rails::Generators::Base
+ desc "Creates an initializer file with default responder configuration"
+
+ def create_responder_initializer
+ create_file "config/initializers/responders.rb", <<-FILE
+class #{Rails.application.class.name}Responder
+ include FlashResponder
+ include HttpCacheResponder
+end
+
+ApplicationController.responder = #{Rails.application.class.name}Responder
+ FILE
+ end
+end
@@ -0,0 +1,53 @@
+class <%= controller_class_name %>Controller < ApplicationController
+<% unless options[:singleton] -%>
+ # GET /<%= table_name %>
+ # GET /<%= table_name %>.xml
+ def index
+ @<%= table_name %> = <%= orm_class.all(class_name) %>
+ respond_with(@<%= table_name %>)
+ end
+<% end -%>
+
+ # GET /<%= table_name %>/1
+ # GET /<%= table_name %>/1.xml
+ def show
+ @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
+ respond_with(@<%= file_name %>)
+ end
+
+ # GET /<%= table_name %>/new
+ # GET /<%= table_name %>/new.xml
+ def new
+ @<%= file_name %> = <%= orm_class.build(class_name) %>
+ respond_with(@<%= file_name %>)
+ end
+
+ # GET /<%= table_name %>/1/edit
+ def edit
+ @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
+ end
+
+ # POST /<%= table_name %>
+ # POST /<%= table_name %>.xml
+ def create
+ @<%= file_name %> = <%= orm_class.build(class_name, "params[:#{file_name}]") %>
+ <%= "flash[:notice] = '#{class_name} was successfully created.' if " if flash? %>@<%= orm_instance.save %>
+ respond_with(@<%= file_name %>)
+ end
+
+ # PUT /<%= table_name %>/1
+ # PUT /<%= table_name %>/1.xml
+ def update
+ @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
+ <%= "flash[:notice] = '#{class_name} was successfully updated.' if " if flash? %>@<%= orm_instance.update_attributes("params[:#{file_name}]") %>
+ respond_with(@<%= file_name %>)
+ end
+
+ # DELETE /<%= table_name %>/1
+ # DELETE /<%= table_name %>/1.xml
+ def destroy
+ @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
+ @<%= orm_instance.destroy %>
+ respond_with(@<%= file_name %>)
+ end
+end
@@ -0,0 +1,4 @@
+module Responders
+ autoload :FlashResponder, 'responders/flash_responder'
+ autoload :HttpCacheResponder, 'responders/http_cache_responder'
+end
Oops, something went wrong.

0 comments on commit 60003b0

Please sign in to comment.