Permalink
Browse files

Finish initial shortening

  • Loading branch information...
1 parent 2edc567 commit e00ee644d4303308a2e094de1d93e965f67aedd2 @joshuaclayton committed Oct 7, 2009
Showing with 5,009 additions and 40 deletions.
  1. +6 −0 app/controllers/peas_controller.rb
  2. +28 −0 app/models/key_generator.rb
  3. +10 −0 app/models/pea.rb
  4. +4 −5 app/views/pages/index.html.erb
  5. +2 −0 app/views/peas/_form.html.erb
  6. 0 app/views/peas/redir.html.erb
  7. +2 −0 app/views/peas/show.html.erb
  8. +4 −35 config/environment.rb
  9. +2 −0 config/routes.rb
  10. +12 −0 features/generate_shortened_urls.feature
  11. +4 −0 features/step_definitions/pea_steps.rb
  12. +8 −0 test/functional/peas_controller_test.rb
  13. +106 −0 vendor/gems/josevalim-inherited_resources-0.9.1/.specification
  14. +101 −0 vendor/gems/josevalim-inherited_resources-0.9.1/CHANGELOG
  15. +20 −0 vendor/gems/josevalim-inherited_resources-0.9.1/MIT-LICENSE
  16. +512 −0 vendor/gems/josevalim-inherited_resources-0.9.1/README.rdoc
  17. +37 −0 vendor/gems/josevalim-inherited_resources-0.9.1/Rakefile
  18. +1 −0 vendor/gems/josevalim-inherited_resources-0.9.1/VERSION
  19. +23 −0 vendor/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources.rb
  20. +74 −0 vendor/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/actions.rb
  21. +42 −0 vendor/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/base.rb
  22. +325 −0 vendor/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/base_helpers.rb
  23. +89 −0 vendor/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/belongs_to_helpers.rb
  24. +334 −0 vendor/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/class_methods.rb
  25. +26 −0 vendor/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/dsl.rb
  26. +20 −0 vendor/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/dumb_responder.rb
  27. +65 −0 vendor/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/has_scope_helpers.rb
  28. +156 −0 vendor/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/legacy/respond_to.rb
  29. +200 −0 vendor/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/legacy/responder.rb
  30. +155 −0 vendor/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/polymorphic_helpers.rb
  31. +95 −0 vendor/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/singleton_helpers.rb
  32. +179 −0 vendor/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/url_helpers.rb
  33. +139 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/aliases_test.rb
  34. +125 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/association_chain_test.rb
  35. +219 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/base_test.rb
  36. +87 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/belongs_to_test.rb
  37. +137 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/class_methods_test.rb
  38. +76 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/customized_belongs_to_test.rb
  39. +70 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/defaults_test.rb
  40. +88 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/flash_test.rb
  41. +112 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/has_scope_test.rb
  42. +108 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/nested_belongs_to_test.rb
  43. +164 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/optional_belongs_to_test.rb
  44. +186 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/polymorphic_test.rb
  45. +51 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/redirect_to_test.rb
  46. +155 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/respond_to_test.rb
  47. +83 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/singleton_test.rb
  48. +30 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/test_helper.rb
  49. +537 −0 vendor/gems/josevalim-inherited_resources-0.9.1/test/url_helpers_test.rb
@@ -0,0 +1,6 @@
+class PeasController < InheritedResources::Base
+ respond_to :html
+ actions :create, :show
+
+ def redir; end
+end
@@ -0,0 +1,28 @@
+class KeyGenerator
+ def self.generate(unique_id)
+ unique_id =- 1
+ available = []
+ available << ("A".."Z").map
+ available << ("a".."z").map
+ available << ("0".."9").map
+
+ available.flatten!
+ result = ""
+
+ code_length = 1
+ cached_result = unique_id
+
+ while (cached_result/available.length.to_f) >= 1
+ code_length += 1
+ cached_result = cached_result/available.length.to_f
+ end
+
+ while code_length > 0
+ idx = (unique_id/(available.length**(code_length - 1))).to_i % available.length
+ result << available[idx]
+ code_length -= 1
+ end
+
+ result
+ end
+end
View
@@ -1,2 +1,12 @@
class Pea < ActiveRecord::Base
+ validates_presence_of :long_url
+ validates_uniqueness_of :key, :long_url
+
+ after_create :build_key
+
+ protected
+
+ def build_key
+ self.update_attribute :key, KeyGenerator.generate(self.id)
+ end
end
@@ -1,5 +1,4 @@
-<form method="get">
- <label for="url">Enter your long URL</label>
- <input type="text" id="url" name="url" placeholder="url" value="" />
- <input type="submit" value="Shorten">
-</form>
+<% form_for Pea.new do |form| %>
+ <%= render :partial => "peas/form", :locals => {:form => form} %>
+ <%= form.submit "Shorten" %>
+<% end %>
@@ -0,0 +1,2 @@
+<%= form.label :long_url, "Enter your long URL" %>
+<%= form.text_field :long_url %>
No changes.
@@ -0,0 +1,2 @@
+<%= h @pea.long_url %> was shortened to:
+<%= link_to peapod_url(:pea_key => @pea.key), peapod_url(:pea_key => @pea.key) %>
View
@@ -1,41 +1,10 @@
-# Be sure to restart your server when you modify this file
-
-# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION
-# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
Rails::Initializer.run do |config|
- # Settings in config/environments/* take precedence over those specified here.
- # Application configuration should go into files in config/initializers
- # -- all .rb files in that directory are automatically loaded.
-
- # Add additional load paths for your own custom dirs
- # config.load_paths += %W( #{RAILS_ROOT}/extras )
-
- # Specify gems that this application depends on and have them installed with rake gems:install
- # config.gem "bj"
- # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
- # config.gem "sqlite3-ruby", :lib => "sqlite3"
- # config.gem "aws-s3", :lib => "aws/s3"
-
- # Only load the plugins named here, in the order given (default is alphabetical).
- # :all can be used as a placeholder for all plugins not explicitly named
- # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
-
- # Skip frameworks you're not going to use. To use Rails without a database,
- # you must remove the Active Record framework.
- # config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
-
- # Activate observers that should always be running
- # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
-
- # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
- # Run "rake -D time" for a list of tasks for finding time zone names.
+ config.gem "josevalim-inherited_resources",
+ :lib => "inherited_resources",
+ :source => "http://gems.github.com"
config.time_zone = 'UTC'
-
- # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
- # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
- # config.i18n.default_locale = :de
-end
+end
View
@@ -1,3 +1,5 @@
ActionController::Routing::Routes.draw do |map|
map.root :controller => "high_voltage/pages", :action => "show", :id => "index"
+ map.resources :peas
+ map.peapod "/:pea_key", :controller => "peas", :action => "redir"
end
@@ -0,0 +1,12 @@
+Feature: Generate shortened URLs
+ In order to successfully shorten URLs
+ As a visitor
+ I want to enter long URLs and get back short URLs
+
+ Scenario: Shorten a given URL
+ When I go to the homepage
+ And I fill in "Enter your long URL" with "http://google.com"
+ And I press "Shorten"
+ Then I should see "http://google.com was shortened to:"
+ And I should see a short URL for "http://google.com"
+
@@ -0,0 +1,4 @@
+Then /^I should see a short URL for "([^\"]*)"$/ do |long_url|
+ pea = Pea.find_by_long_url(long_url)
+ assert_select("a[href=?]", peapod_url(pea.key))
+end
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class PeasControllerTest < ActionController::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
@@ -0,0 +1,106 @@
+--- !ruby/object:Gem::Specification
+name: josevalim-inherited_resources
+version: !ruby/object:Gem::Version
+ version: 0.9.1
+platform: ruby
+authors:
+- "Jos\xC3\xA9 Valim"
+autorequire:
+bindir: bin
+cert_chain: []
+
+date: 2009-09-01 00:00:00 -04:00
+default_executable:
+dependencies: []
+
+description: Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important.
+email: jose.valim@gmail.com
+executables: []
+
+extensions: []
+
+extra_rdoc_files:
+- README.rdoc
+files:
+- CHANGELOG
+- MIT-LICENSE
+- README.rdoc
+- Rakefile
+- VERSION
+- lib/inherited_resources.rb
+- lib/inherited_resources/actions.rb
+- lib/inherited_resources/base.rb
+- lib/inherited_resources/base_helpers.rb
+- lib/inherited_resources/belongs_to_helpers.rb
+- lib/inherited_resources/class_methods.rb
+- lib/inherited_resources/dsl.rb
+- lib/inherited_resources/dumb_responder.rb
+- lib/inherited_resources/has_scope_helpers.rb
+- lib/inherited_resources/legacy/respond_to.rb
+- lib/inherited_resources/legacy/responder.rb
+- lib/inherited_resources/polymorphic_helpers.rb
+- lib/inherited_resources/singleton_helpers.rb
+- lib/inherited_resources/url_helpers.rb
+- test/respond_to_test.rb
+- test/customized_belongs_to_test.rb
+- test/nested_belongs_to_test.rb
+- test/base_test.rb
+- test/redirect_to_test.rb
+- test/has_scope_test.rb
+- test/class_methods_test.rb
+- test/association_chain_test.rb
+- test/aliases_test.rb
+- test/flash_test.rb
+- test/url_helpers_test.rb
+- test/belongs_to_test.rb
+- test/polymorphic_test.rb
+- test/defaults_test.rb
+- test/singleton_test.rb
+- test/optional_belongs_to_test.rb
+- test/test_helper.rb
+has_rdoc: true
+homepage: http://github.com/josevalim/inherited_resources
+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: inherited_resources
+rubygems_version: 1.3.5
+signing_key:
+specification_version: 3
+summary: Inherited Resources speeds up development by making your controllers inherit all restful actions so you just have to focus on what is important.
+test_files:
+- test/respond_to_test.rb
+- test/customized_belongs_to_test.rb
+- test/nested_belongs_to_test.rb
+- test/base_test.rb
+- test/redirect_to_test.rb
+- test/has_scope_test.rb
+- test/class_methods_test.rb
+- test/association_chain_test.rb
+- test/aliases_test.rb
+- test/flash_test.rb
+- test/url_helpers_test.rb
+- test/belongs_to_test.rb
+- test/polymorphic_test.rb
+- test/defaults_test.rb
+- test/singleton_test.rb
+- test/optional_belongs_to_test.rb
+- test/test_helper.rb
@@ -0,0 +1,101 @@
+# Version 0.9
+
+* Backported ActionController::Responder from Rails 3;
+* Added parent_url helper;
+* Added association_chain helper (as suggested by http://github.com/emmanuel);
+
+# Version 0.8
+
+* Fixed a small bug on optional belongs to with namespaced controllers.
+* Allow a parameter to be given to collection_url in polymorphic cases to replace
+ the parent.
+* Allow InheritedResources to be called without inheritance.
+* Ensure that controllers that inherit from a controller with InheritedResources
+ works properly.
+
+# Version 0.7
+
+* Allow procs as default value in has scope to be able to use values from session, for example.
+* Allow blocks with arity 0 or -1 to be given as the redirect url:
+
+ def destroy
+ destroy!{ project_url(@project) }
+ end
+
+* Allow interpolation_options to be set in the application controller.
+* Added has_scope to controller (an interface for named_scopes).
+* Added polymorphic_belongs_to, optional_belongs_to and singleton_belongs_to
+ as quick methods.
+* Only load belongs_to, singleton and polymorphic helpers if they are actually
+ required. base_helpers, class_methods, dumb_responder and url_helpers are loaded
+ when you inherited from base for the first time.
+
+# Version 0.6
+
+* Ensure that the default template is not rendered if the default_template_format
+ is not accepted. This is somehow related with the security breach report:
+
+ http://www.rorsecurity.info/journal/2009/4/24/hidden-actions-render-templates.html
+
+ IR forbids based on mime types. For example: respond_to :html, :except => :index
+ ensures that the index.html.erb view is not rendered, making your IR controllers
+ safer.
+
+* Fixed a bug that happens only when format.xml is given to blocks and then it
+ acts as default, instead of format.html.
+* Fixed a strange bug where when you have create.html.erb or update.html.erb,
+ it makes IE6 and IE7 return unprocessable entity (because they send Mime::ALL).
+* Stop rescueing any error when constantizing the resource class and allow
+ route_prefix to be nil.
+* Cleaned up tests and responder structure. Whenever you pass a block to aliases
+ and this block responds to the request, the other blocks are not parsed improving performance.
+* [BACKWARDS INCOMPATIBLE] By default, Inherited Resources respond only :html requests.
+* Added a quick way to overwrite the redirect to url in :create, :update and :destroy.
+
+# Version 0.5
+
+* Decoupled routes name from :instance_name and :collection_name. This way we
+ have more flexibility. Use route_instance_name and route_collection_name to
+ to change routes.
+* Avoid calling human_name on nil when a resource class is not defined.
+* Only call I18n if it's defined.
+
+# Version 0.4
+
+* Dealing with namespaced controllers out of the box.
+* Added support to namespaced routes through :route_prefix.
+* Added fix when resource_url is not defined.
+* Added better handling for namespaced controllers.
+* Added flash messages scoped by namespaced controllers.
+* Deprecated {{resource}} in I18n, use {{resource_name}} instead.
+* rspec bug fix is not automatically required anymore. User has to do it
+ explicitly.
+* Added a file which fix a rspec bug when render is called inside a method
+ which receives a block.
+* parent? does not take begin_of_association_chain into account anymore
+* Added options to url helpers.
+* Added :optional to belongs_to associations. It allows you to deal with
+ categories/1/products/2 and /products/2 with just one controller.
+* Cleaned up tests.
+
+# Version 0.3
+
+* Minor bump after three bug fixes.
+* Bug fix when showing warning of constant redefinition.
+* Bug fix with ApplicationController not being unloaded properly on development.
+* Bug fix when having root singleton resources. Calling collection_url would
+ raise "NoMethodError _url", not it will call root_url.
+* More comments on UrlHelpers.
+
+# Version 0.2
+
+* Bug fix when ApplicationController is already loaded when we load respond_to.
+* Added support success/failure blocks.
+* Eager loading of files to work properly in multithreaded environments.
+
+# Version 0.1
+
+* Added more helper_methods.
+* Added Rails 2.3.0 and changed tests to work with ActionController::TestCase.
+* First release. Support to I18n, singleton controllers, polymorphic
+controllers, belongs_to, nested_belongs_to and url helpers.
@@ -0,0 +1,20 @@
+Copyright (c) 2009 José Valim
+
+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.
Oops, something went wrong.

0 comments on commit e00ee64

Please sign in to comment.