Skip to content

Commit

Permalink
adding captcha to prevent spammy signups
Browse files Browse the repository at this point in the history
  • Loading branch information
blueroot committed Oct 12, 2011
1 parent 67bb300 commit 3f3caa8
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 266 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ gem 'sunspot_rails', '1.2.1'
#gem 'feedtools', :path => 'vendor/gems/feedtools'
gem 'simple-rss'
gem 'paperclip', '2.3.5'
gem "recaptcha", :require => "recaptcha/rails"

group :test, :development do
gem 'railsmachine'
Expand Down
22 changes: 13 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GIT
remote: git://github.com/sferik/rails_admin.git
revision: 77b45592f4bbcc182294e4281147671c74a9179d
revision: 1d1622a192786f416b35cc0e9e047806710e4888
branch: rails-3.0
specs:
rails_admin (0.0.1)
Expand Down Expand Up @@ -46,7 +46,7 @@ GEM
arel (2.0.10)
bcrypt-ruby (2.1.4)
builder (2.1.2)
capistrano (2.8.0)
capistrano (2.9.0)
highline
net-scp (>= 1.0.0)
net-sftp (>= 2.0.0)
Expand All @@ -70,7 +70,7 @@ GEM
actionpack (>= 2.3.7)
activesupport (>= 2.3.7)
i18n (>= 0.4.0)
haml (3.1.2)
haml (3.1.3)
has_scope (0.5.1)
highline (1.6.2)
i18n (0.5.0)
Expand All @@ -80,6 +80,7 @@ GEM
jquery-rails (0.2.5)
rails (~> 3.0)
thor (~> 0.14.4)
json (1.6.1)
mail (2.2.19)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
Expand All @@ -94,7 +95,7 @@ GEM
net-ssh (>= 1.99.1)
net-sftp (2.0.5)
net-ssh (>= 2.0.9)
net-ssh (2.2.0)
net-ssh (2.2.1)
net-ssh-gateway (1.1.0)
net-ssh (>= 1.99.1)
nokogiri (1.4.7)
Expand Down Expand Up @@ -138,7 +139,7 @@ GEM
polyglot (0.3.2)
pr_geohash (1.0.0)
pyu-ruby-sasl (0.0.3.3)
rack (1.2.3)
rack (1.2.4)
rack-mount (0.6.14)
rack (>= 1.0.0)
rack-openid (1.2.0)
Expand All @@ -163,9 +164,11 @@ GEM
rdoc (~> 3.4)
thor (~> 0.14.4)
rake (0.9.2)
rdoc (3.9.2)
rdoc (3.10)
json (~> 1.4)
recaptcha (0.3.1)
responders (0.6.4)
rest-client (1.6.3)
rest-client (1.6.7)
mime-types (>= 1.16)
rsolr (0.12.1)
builder (>= 2.1.2)
Expand All @@ -189,8 +192,8 @@ GEM
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.29)
warden (1.0.5)
tzinfo (0.3.30)
warden (1.0.6)
rack (>= 1.0)
will_paginate (3.0.pre2)
yamler (0.1.0)
Expand All @@ -214,6 +217,7 @@ DEPENDENCIES
rails_admin!
railsmachine
rake (= 0.9.2)
recaptcha
simple-navigation (= 3.0.2)
simple-rss
sunspot_rails (= 1.2.1)
Expand Down
243 changes: 9 additions & 234 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
== Skeleton for new Rails 3 based application
== OpenMissouri Project

This simple application includes ruby/rails technology which we use in the Flatsoft for new projects.
Application currently based on Rails 3 stable branch.
OpenMissouri is a simple application that runs openmissouri.org.

=== Gems
OpenMissouri.org helps connect you to data that's stored offline by state and local government agencies. We make it easy for you to find, request and share information about these hundreds of offline data sets.

The OpenMissouri Project is built with Rails 3 and Ruby 1.9.2.


=== Some gems that are used

* Basic auth: Devise http://github.com/plataformatec/devise, app/models/user.rb
* OAuth auth: Omniauth https://github.com/intridea/omniauth, app/models/user/omni_auth_extension.rb
Expand All @@ -17,7 +21,7 @@ Application currently based on Rails 3 stable branch.
* Tests: RSpec, Shoulda, RR, Cucumber, Factory Girl, Autotest
* Code metrics: rails_best_practices, http://github.com/flyerhzm/rails_best_practices and rcov

=== Initializers
=== Initializers you might want to modify

* config.rb - loads configuration for current Rails environment to the configatron, so you can access them like configatron.app_name
* devise.rb - setup devise options, mailer_sender and pepper from config
Expand Down Expand Up @@ -56,232 +60,3 @@ Scaffold generator will create: model with rspec, factory, controller based on i
Cucumber:feature generator will create: cucumber feature for scaffold resource

$ rails g cucumber:feature post title:string text:text

=== Tests

We use rspec with shoulda matchers for model testing and cucumber with capybara
for integration testing.

==== Rspec

You should cover validations, associations with shoulda matchers and test deeply
complected model methods.
Check out for example user_spec.rb

describe User do
it { should allow_mass_assignment_of(:full_name) }
it { should allow_mass_assignment_of(:email) }
it { should allow_mass_assignment_of(:password) }
it { should allow_mass_assignment_of(:password_confirmation) }

it { should validate_presence_of :full_name }
end

Use shortcuts specify {}, it {} and subject {}

subject { @user.address }
it { should be_valid }

Start context with ‘when’/'with’ and methods description with ‘#’

Use RSpec matchers to get meaningful messages
specify { user.should be_valid }

Only one expectation per it block

describe DemoMan do
before(:all) do
@demo_man = DemoMan.new
end

subject { @demo_man }

it { should respond_to :name }
it { should respond_to :gender }
it { should respond_to :age }
end

(Over)use describe and context

describe User do
before { @user = User.new }

subject { @user }

context "when name empty" do
it { should not be_valid }
specify { @user.save.should == false }
end

context "when name not empty" do
before { @user.name = 'Sam' }

it { should be_valid }
specify { @user.save.should == true }
end

describe :present do
subject { @user.present }

context "when user is a W" do
before { @user.gender = 'W' }

it { should be_a Flower }
end

context "when user is a M" do
before { @user.gender = 'M' }

it { should be_an IMac }
end
end
end

Test Valid, Edge and Invalid cases

describe "#month_in_english(month_id)" do
context "when valid" do
it "should return 'January' for 1" # lower boundary
it "should return 'March' for 3"
it "should return 'December' for 12" # upper boundary
context "when invalid" do
it "should return nil for 0"
it "should return nil for 13"
end
end

http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/

==== Cucumber features

==== Organization

Group steps by model. We’ve found the best way to keep track of them is to group
them by the primary model they affect. Some steps may affect multiple models,
but usually there is an obvious choice.

Put each feature in it’s own file. Don’t be afraid to put features in subdirectories.
For any large app, it’s almost essential.

Keep the file organized grouping the steps by Given / When / Then.

Do not overload the files generated by Cucumber like step_definitions/web_steps.rb
and support/env.rb with your own steps, helpers or setup code. These files are
likely to get overwritten when you update Cucumber so store your stuff in your own files.


==== Custom steps make your scenario DRY and accessible

Scenarios should have the same lifecyle as your code: Red, Green, Refactor to make
them DRY and easy to read.

Group multiple steps together. For instance:
When I fill in "Title" with "New title"
And I fill in "Text" with "New text"
And I press "Submit"
could be refactored to:
When I submit valid post details

==== Background: setup the DRY way

Make the feature focus on one business object/action/context and the background
will get longer than the scenarios.

Use 'Background' to consolidate common steps in a feature:
Background:
Given I am an authenticated user

==== Factory Girl steps

Factory girl comes with some really useful cucumber steps.
Try to use Factories as match as possible. For example if you have Post with
different states like confirmed and unconfirmed it's good practice create two factories for this cases:

Factory.define :post do |f|
f.title 'How to write a good post'
f.text 'Lorem ipsum?'
f.confirmed true
end

Factory.define :unconfirmed_post, :parent => :post do |f|
f.confirmed false
end

And then use them in the steps:
Given a post exists
Given a unconfirmed post exists

==== Tags

You can use @webmock for enabling webmock functionality around some scenario. It's mean all real HTTP connection will be
disabled in the given scenario and you need to stub them with stub_request. Checkout twitter_authentication_steps.rb for example.

You can use @vcr for enabling VCR functionality around some scenario. It will also disable all real HTTP connectons
and will try to replay them from the cassette assotiated with this scenario. Each cassette will be stored in the
features/cassettes/:feature_name/:scenario_name.

Checkout also:

* http://collectiveidea.com/blog/archives/2010/09/09/practical-cucumber-factory-girl-steps/
* http://collectiveidea.com/blog/archives/2010/09/13/practical-cucumber-organization/
* http://eggsonbread.com/2010/09/06/my-cucumber-best-practices-and-tips/
* http://github.com/aslakhellesoy/cucumber/wiki/Tutorials-and-Related-Blog-Posts

=== Formtastic

If you have any difficulties with fortastic fill free to use standard form helpers.
But better to extend fortastic with custom field types.

# lib/formtastic/money.rb
module Formtastic
module Money
protected

def money_input(method, options = {})
html_options = { :size => 6 }.merge(options.delete(:input_html) || {})
html_options = default_string_options(method, :string).merge(html_options)

self.label(method, options_for_label(options)) <<
template.content_tag(:span, '$', :class => 'char') <<
self.text_field(method, html_options)
end
end
end

Formtastic::SemanticFormBuilder.send(:include, Formtastic::Money)

# config/initialisers/formtastic.rb
require 'formtastic/money'

# in the view
<%= form.input :price, :as => :money %>

=== Simple Navigation

Navigation configuration stored in the config/navigations folder.
We have main and user navigation which accessible for visitor and authorized user accordingly.


=== Note on Patches/Pull Requests

* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a future version unintentionally.
* Commit, do not mess with rakefile, version, or history.
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
* Send a pull request. Bonus points for topic branches.
* Also you can send bug reports in the PivotalProject https://www.pivotaltracker.com/projects/140789


=== How to update existing project with new changes from rails3-base repo

git remote add rails3-base git://github.com/fs/rails3-base.git
git checkout -b rails3-base-update
git pull rails3-base master
# fix conflicts
# commit
# test
# merge

Thanks,
Flatsoft
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ApplicationController < ActionController::Base
def prepare_global_sidebar
@categories = Category.published_categories
@feed = SimpleRSS.parse open('http://blog.openmissouri.org/?feed=rss2')

end

def build_footer_links
Expand Down
13 changes: 13 additions & 0 deletions app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class RegistrationsController < Devise::RegistrationsController
def create

if verify_recaptcha
super
else
build_resource
clean_up_passwords(resource)
flash[:alert] = "There was an error with the recaptcha code below. Please re-enter the code."
render_with_scope :new
end
end
end
4 changes: 2 additions & 2 deletions app/views/data_sets/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@
<% if @data_set.samplefile? %>
<p>
<b>Sample data file submitted by user:</b>
<%= link_to "Download", @data_set.samplefile.url %>
<b>Download a sample data file:</b>
<%= link_to "Download sample data", @data_set.samplefile.url, {:class => "small orange awesome"} %>
</p>
<% end%>

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
<%= form.input :password, :required => true %>
<%= form.input :password_confirmation, :required => true %>
<% end %>
<%= recaptcha_tags %>
<% end %>
<%= form.buttons do %>
<%= form.commit_button 'Sign up' %>
<% end %>
<% end %>
Expand Down
Loading

0 comments on commit 3f3caa8

Please sign in to comment.