diff --git a/.gitignore b/.gitignore index a2f6bf5..56813f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,21 +1,5 @@ pkg/ Gemfile.lock .rspec -spec/tmp -spec/dummy/log/*.log -spec/dummy/tmp/ -spec/dummy/config/*.yml -spec/dummy/config/*.conf -spec/dummy/config/logrotate-config -spec/dummy/config/nginx-config -spec/dummy/db/schema.rb -spec/dummy/db/sphinx/ -spec/dummy/public/cache/ -spec/dummy/public/assets/ -spec/dummy/public/ckeditor_assets/ -spec/dummy/public/uploads/ -spec/dummy/vendor/bundle/ -spec/dummy/app/helpers/manage -spec/dummy/config/database.yml -spec/dummy/log/development.log -.svn \ No newline at end of file +test/dummy +.svn diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8b66bee --- /dev/null +++ b/.travis.yml @@ -0,0 +1,34 @@ +--- +language: ruby +sudo: false +cache: bundler + +rvm: + - '2.2.6' + - '2.3.3' + - 'ruby-head' + +env: + - DB=sqlite RAILS='~> 5.0' + - DB=postgresql RAILS='~> 5.0' + - DB=mysql RAILS='~> 5.0' + - DB=sqlite RAILS='~> 5.0' SEQUEL=1 + - DB=sqlite RAILS='~> 4.2' + - DB=postgresql RAILS='~> 4.2' + - DB=mysql RAILS='~> 4.2' + +matrix: + allow_failures: + - rvm: ruby-head + +before_script: + # - mysql -e 'create database myapp_test' + # - psql -c 'create database myapp_test' -U postgres + - bundle exec rake dummy:setup + - bundle exec rake app:db:create + - bundle exec rake app:db:migrate + - bundle exec rake app:db:migrate RAILS_ENV=test + +script: + - bundle exec rake test + diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..061868f --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,120 @@ +# Change Log + +## [v0.5.0](https://github.com/pludoni/simple-captcha/tree/v0.4.3) (2019-09-09) + +- Feature: supports Sequel #63 by cmrd-senya +- Fix: Order of font is important; switched order of font + label call #62 by SuperTux88 +- Fix: catches Deadlock #65 + +## [v0.4.3](https://github.com/pludoni/simple-captcha/tree/v0.4.3) (2017-02-20) + +- Allow manual font family overrides #55 to help user on Debian testing with ImageMagick Version 6.9.6 + +## [v0.4.2](https://github.com/pludoni/simple-captcha/tree/v0.4.2) (2017-02-04) + +- Rails 5 compatibility https://github.com/pludoni/simple-captcha/pull/53 by [deivid-rodriguez](https://github.com/deivid-rodriguez) +- For reload button can now also drop jQuery and rely on Prototype or Plain Javascript https://github.com/pludoni/simple-captcha/pull/51 by [bfbones](https://github.com/bfbones) + +## [v0.4.0](https://github.com/pludoni/simple-captcha/tree/v0.4.0) (2015-12-29) +[Full Changelog](https://github.com/pludoni/simple-captcha/compare/v0.3.4...v0.4.0) + +**Closed issues:** + +- Readability of generated image [\#19](https://github.com/pludoni/simple-captcha/issues/19) + +**Merged pull requests:** + +- Fix mass-assignment error when using protected\_attributes [\#38](https://github.com/pludoni/simple-captcha/pull/38) ([soylent](https://github.com/soylent)) +- Add autocorrect = 'off' and autocapitalize = 'off' for iPhone/iPad [\#48](https://github.com/pludoni/simple-captcha/pull/48) ([Friend-LGA](https://github.com/Friend-LGA)) +- fix no session request\(api etc\) will never delete expired captcha data [\#46](https://github.com/pludoni/simple-captcha/pull/46) ([beviz](https://github.com/beviz)) +- Remove obsolete info from README.md [\#41](https://github.com/pludoni/simple-captcha/pull/41) ([rubyconvict](https://github.com/rubyconvict)) +- Add optional possibility to generate captcha partial with HAML [\#40](https://github.com/pludoni/simple-captcha/pull/40) ([aliaksandrb](https://github.com/aliaksandrb)) + +## [v0.3.4](https://github.com/pludoni/simple-captcha/tree/v0.3.4) (2015-02-05) +[Full Changelog](https://github.com/pludoni/simple-captcha/compare/v0.3.3...v0.3.4) + +**Closed issues:** + +- Refresh on "Model Based" does not work because of prefixed id on captcha key field [\#29](https://github.com/pludoni/simple-captcha/issues/29) + +**Merged pull requests:** + +- Little improvements [\#39](https://github.com/pludoni/simple-captcha/pull/39) ([kuldeepaggarwal](https://github.com/kuldeepaggarwal)) + +## [v0.3.3](https://github.com/pludoni/simple-captcha/tree/v0.3.3) (2015-01-28) +[Full Changelog](https://github.com/pludoni/simple-captcha/compare/v0.3.2...v0.3.3) + +**Closed issues:** + +- Integration with Devise [\#32](https://github.com/pludoni/simple-captcha/issues/32) +- 404 when image URL is not loaded from rails page [\#24](https://github.com/pludoni/simple-captcha/issues/24) +- Add a class to the text field [\#21](https://github.com/pludoni/simple-captcha/issues/21) +- when i run rails generate simple\_captcha i get "simple\_captcha\_generator.rb:18:in `create\_migration': wrong number of arguments \(3 for 0\) \(ArgumentError\)" [\#18](https://github.com/pludoni/simple-captcha/issues/18) + +**Merged pull requests:** + +- Add additional check for request [\#36](https://github.com/pludoni/simple-captcha/pull/36) ([incubus](https://github.com/incubus)) +- Ruby 2.2.0: Fix warning [\#35](https://github.com/pludoni/simple-captcha/pull/35) ([fmbiete](https://github.com/fmbiete)) +- Remove the executable bit from image.rb [\#31](https://github.com/pludoni/simple-captcha/pull/31) ([incubus](https://github.com/incubus)) +- Add noise to image [\#28](https://github.com/pludoni/simple-captcha/pull/28) ([lionello](https://github.com/lionello)) +- Remove the need for tempfiles [\#27](https://github.com/pludoni/simple-captcha/pull/27) ([lionello](https://github.com/lionello)) +- Using absolute URL for img src to support inclusion in non-rails pages [\#26](https://github.com/pludoni/simple-captcha/pull/26) ([lionello](https://github.com/lionello)) +- add i18n example for refresh button text [\#23](https://github.com/pludoni/simple-captcha/pull/23) ([Feuda](https://github.com/Feuda)) +- Add possibility to set `implode` valude for the generated image [\#20](https://github.com/pludoni/simple-captcha/pull/20) ([mrhead](https://github.com/mrhead)) +- Updated README.md [\#16](https://github.com/pludoni/simple-captcha/pull/16) ([rubyconvict](https://github.com/rubyconvict)) + +## [v0.3.2](https://github.com/pludoni/simple-captcha/tree/v0.3.2) (2014-06-12) +[Full Changelog](https://github.com/pludoni/simple-captcha/compare/v0.3.1...v0.3.2) + +**Closed issues:** + +- undefined method `apply\_simple\_captcha' for User:Class \(NoMethodError\) [\#14](https://github.com/pludoni/simple-captcha/issues/14) + +**Merged pull requests:** + +- Fixes \#14 - undefined method 'apply\_simple\_captcha' \(Mongoid\) [\#15](https://github.com/pludoni/simple-captcha/pull/15) ([rubyconvict](https://github.com/rubyconvict)) + +## [v0.3.1](https://github.com/pludoni/simple-captcha/tree/v0.3.1) (2014-05-24) +[Full Changelog](https://github.com/pludoni/simple-captcha/compare/v0.2.2...v0.3.1) + +**Closed issues:** + +- simple\_captcha\_valid? is self destructing or something? [\#13](https://github.com/pludoni/simple-captcha/issues/13) +- Gem breaks when middleware placed after mobvious in middleware stack [\#9](https://github.com/pludoni/simple-captcha/issues/9) + +## [v0.2.2](https://github.com/pludoni/simple-captcha/tree/v0.2.2) (2014-05-04) +[Full Changelog](https://github.com/pludoni/simple-captcha/compare/v0.3.0...v0.2.2) + +## [v0.3.0](https://github.com/pludoni/simple-captcha/tree/v0.3.0) (2014-05-04) +[Full Changelog](https://github.com/pludoni/simple-captcha/compare/v0.2.1...v0.3.0) + +**Closed issues:** + +- Field key doesn't have a default value [\#7](https://github.com/pludoni/simple-captcha/issues/7) + +**Merged pull requests:** + +- fix: avoid 'create\_migration' method conflict [\#11](https://github.com/pludoni/simple-captcha/pull/11) ([mzaharie](https://github.com/mzaharie)) +- Support for using the same captcha in multiple forms of the same page [\#8](https://github.com/pludoni/simple-captcha/pull/8) ([fmbiete](https://github.com/fmbiete)) + +## [v0.2.1](https://github.com/pludoni/simple-captcha/tree/v0.2.1) (2013-12-29) +[Full Changelog](https://github.com/pludoni/simple-captcha/compare/v0.2.0...v0.2.1) + +**Closed issues:** + +- What about publishing a new gem? [\#6](https://github.com/pludoni/simple-captcha/issues/6) +- Captcha image wont show up on Rails 4 [\#1](https://github.com/pludoni/simple-captcha/issues/1) + +## [v0.2.0](https://github.com/pludoni/simple-captcha/tree/v0.2.0) (2013-12-01) +[Full Changelog](https://github.com/pludoni/simple-captcha/compare/v0.1.6...v0.2.0) + +**Merged pull requests:** + +- Fix default gemfile for dummy application [\#5](https://github.com/pludoni/simple-captcha/pull/5) ([freemanoid](https://github.com/freemanoid)) +- Correct contributing part of README according to travis config [\#4](https://github.com/pludoni/simple-captcha/pull/4) ([freemanoid](https://github.com/freemanoid)) +- Add refresh image button [\#3](https://github.com/pludoni/simple-captcha/pull/3) ([freemanoid](https://github.com/freemanoid)) + +## [v0.1.6](https://github.com/pludoni/simple-captcha/tree/v0.1.6) (2013-06-24) + + +\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..ee66450 --- /dev/null +++ b/Gemfile @@ -0,0 +1,25 @@ +source "https://rubygems.org" + +if ENV['SEQUEL'] + gem "sequel", "~> 5.14" + gem "sequel-rails", "~> 1.0" +end + +gemspec :name => 'simple_captcha2' + +gem 'rails', ENV['RAILS'] || '~> 5.0' +gem 'listen' +gem 'jquery-rails' +gem 'formtastic' + +if ENV['DB'] and ENV['DB']['mysql'] + gem 'mysql2' +end + +if ENV['DB'] and ENV['DB']['postgresql'] + gem 'pg' +end + +gem "bootsnap", "~> 1.3" + +gem "puma", "~> 3.12" diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..a9122d7 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,27 @@ +The MIT License (MIT) +===================== + +Copyright © 2009 Chad Humphries, David Chelimsky +Copyright © 2006 David Chelimsky, The RSpec Development Team +Copyright © 2005 Steven Baker + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..0dfef07 --- /dev/null +++ b/README.md @@ -0,0 +1,365 @@ +# SimpleCaptcha2 + +[![Build Status](https://travis-ci.org/pludoni/simple-captcha.png?branch=master)](https://travis-ci.org/pludoni/simple-captcha) +[![Gem Version](https://badge.fury.io/rb/simple_captcha2.svg)](https://badge.fury.io/rb/simple_captcha2) + +SimpleCaptcha(2) is the simplest and a robust captcha plugin. Its implementation requires adding up a single line in views and in controllers/models. +SimpleCaptcha2 is available to be used with Rails 3 + 4. +This is a fork of the popular Rubygem ``simple_captcha`` which got abandoned. + +## Features + +* Zero FileSystem usage (secret code moved to db-store and image storage removed). +* Provides various image styles. +* Provides three level of complexity of images. +* Works absolutely fine in distributed environment(session and db based implementation works fine in distributed environment). +* Implementation is as easy as just writing a single line in your view. ```<%= show_simple_captcha %>``` within the 'form' tags. +* Flexible DOM and CSS handling(There is a separate view partial for rendering SimpleCaptcha DOM elements). +* Automated removal of 1 hour old unmatched simple_captcha data. + +## Requirements + +* Ruby >= 1.9.3 +* Rails >= 3.2 +* ImageMagick should be installed on your machine to use this plugin. + visit http://www.imagemagick.org/script/index.php for more details. + + + +## Installation + +You might need to install Ghostscript on a Mac-System or a Debian-System: + +``` +brew install ghostscript +``` + +``` +apt-get install ghostscript +``` + +The default font, that imagemagick uses is Arial. Make sure that font is available (``apt install ttf-mscorefonts-installer``) or change the font in the SimpleCaptcha config. + +Put this into your Gemfile + +```ruby +gem 'simple_captcha2', require: 'simple_captcha' +``` + +and run ``bundle install``. + +## Setup + +After installation, follow these simple steps to setup the plugin. The setup will depend on the version of rails your application is using. + +```bash +rails generate simple_captcha [template_format] # Available options erb, haml. Default: erb +rake db:migrate # Mongoid: skip this step and remove the migration +``` + +## Usage + +There are two usage scenarios: + +### Controller Based + +Add the following line in the file "app/controllers/application.rb" + +```ruby +ApplicationController < ActionController::Base + include SimpleCaptcha::ControllerHelpers +end +``` + +In the view file within the form tags add this code + +```erb +<%= show_simple_captcha %> +``` + +and in the controller's action authenticate it as + +```ruby +if simple_captcha_valid? + do this +else + do that +end +``` + +### Model Based + +This is suggested, if you want to integrate the error message into the normal form validation flow. + +In the view file within the form tags write this code + +```erb +<%= show_simple_captcha(:object=>"user") %> +``` + +and in the model class add this code + +```ruby +class User < ActiveRecord::Base + apply_simple_captcha +end +``` + +Mongoid: + +```ruby +class User + include SimpleCaptcha::ModelHelpers + apply_simple_captcha +end +``` + +#### Strong parameters (Rails 4.x) + +Must add them: + +```ruby +:captcha, :captcha_key +``` + +#### Form-Builder helper + +```erb +<%= form_for @user do |form| -%> + ... + <%= form.simple_captcha :label => "Enter numbers.." %> + ... +<% end -%> +``` + +#### Validating with captcha + +NOTE: @user.valid? will still work as it should, it will not validate the captcha code. + +```ruby +@user.valid_with_captcha? +``` + +#### Saving with captcha + +NOTE: @user.save will still work as it should, it will not validate the captcha code. + +```ruby +@user.save_with_captcha +``` + +### Formtastic integration + +SimpleCaptcha detects if you are using Formtastic: + +```erb +<%= form.input :captcha, :as => :simple_captcha %> +``` + +### Tests + +You can make the Captcha always pass with a initializer file: config/initializers/simple_captcha.rb + +```ruby +SimpleCaptcha.always_pass = Rails.env.test? +``` + +You can also ask for the value, e.g. Acceptance Tests/Features: + +```ruby +visit '/pages/form_tag' +assert_equal 1, SimpleCaptcha::SimpleCaptchaData.count +fill_in 'captcha', with: SimpleCaptcha::SimpleCaptchaData.first.value +``` + +## ORM support + +simple-captcha2 supports 3 type of ORM: ActiveRecord, Sequel and Mongoid. + +Selection of ORM is base on loaded classes. If `ActiveRecord` is loaded then it will be used for the simple captcha data model. +If `ActiveRecord` is undefined, `Sequel` presence is tested. If `Sequel` is defined it will used for the simple captcha data model. +If not, `Mongoid` is used. + +For instance if your application is using Sequel as an ORM just make sure you require `sequel-rails` gem before `simple-captcha2` + in your Gemfile and respective model will be selected automatically. + +## Options & Examples + +### View Options + +* ``:label`` - provides the custom text b/w the image and the text field, the default is "type the code from the image" +* ``:object`` - the name of the object of the model class, to implement the model based captcha. +* ``:code_type`` - return numeric only if set to 'numeric' +* ``:multiple`` - allow to use the same captcha in multiple forms in one page. True for the first appaerance and false for the rest. + +### Global options + +* ``:image_style`` - provides the specific image style for the captcha image. +There are eight different styles available with the plugin as... + +1. simply_blue +2. simply_red +3. simply_green +4. charcoal_grey +5. embosed_silver +6. all_black +7. distorted_black +8. almost_invisible + +Default style is 'simply_blue'. +You can also specify 'random' to select the random image style. + +* ``:distortion`` - handles the complexity of the image. The :distortion can be set to 'low', 'medium' or 'high'. Default is 'low'. + +* ``:implode`` - handles the complexity of the image. The :implode can be set to 'none', 'low', 'medium' or 'high'. Default is 'medium'. + +Create "./config/initializers/simple_captcha.rb" + +```ruby +SimpleCaptcha.setup do |sc| + # default: 100x28 + sc.image_size = '120x40' + + # default: 5 + sc.length = 6 + + # default: simply_blue + # possible values: + # 'embosed_silver', + # 'simply_red', + # 'simply_green', + # 'simply_blue', + # 'distorted_black', + # 'all_black', + # 'charcoal_grey', + # 'almost_invisible' + # 'random' + sc.image_style = 'simply_green' + + # default: low + # possible values: 'low', 'medium', 'high', 'random' + sc.distortion = 'medium' + + # default: medium + # possible values: 'none', 'low', 'medium', 'high' + sc.implode = 'low' +end +``` + +You can add your own style: + +```ruby +SimpleCaptcha.setup do |sc| + sc.image_style = 'mycaptha' + sc.add_image_style('mycaptha', [ + "-background '#F4F7F8'", + "-fill '#86818B'", + "-border 1", + "-bordercolor '#E0E2E3'"]) +end +``` + +You can provide the path where image_magick is installed as well: + +```ruby +SimpleCaptcha.setup do |sc| + sc.image_magick_path = '/usr/bin' # you can check this from console by running: which convert +end +``` + +You can setup in which format the reload of the captcha is executed: + +```ruby +SimpleCaptcha.setup do |sc| + sc.refresh_format = :prototype # or :jquery, or :plain_javascript default is :jquery +end +``` + +If needed, you can explicitly state the font used for generating the captcha: + +```ruby +SimpleCaptcha.setup do |sc| + sc.font = "DejaVu-Sans" +end +``` + +### How to change the CSS for SimpleCaptcha DOM elements? + +You can change the CSS of the SimpleCaptcha DOM elements as per your need in this file. + +``app/views/simple_captcha/_simple_captcha.erb`` + +### View's Examples +#### Controller Based Example + +```erb +<%= show_simple_captcha %> + +<%= show_simple_captcha(:label => "human authentication") %> +``` + +#### Model Based Example + +```erb +<%= show_simple_captcha(:object => 'user', :label => "human authentication") %> +``` + +#### Model Options + +* ``:message`` - provides the custom message on failure of captcha authentication the default is "Secret Code did not match with the Image" + +* ``:add_to_base`` - if set to true, appends the error message to the base. + +##### Model's Example + +```ruby +class User < ActiveRecord::Base + apply_simple_captcha +end + +class User < ActiveRecord::Base + apply_simple_captcha :message => "The secret Image and code were different", :add_to_base => true +end +``` + + +## I18n + +```yaml +en: + simple_captcha: + placeholder: "Enter the image value" + label: "Enter the code in the box:" + refresh_button_text: "Refresh" + message: + default: "Secret Code did not match with the Image" + user: "The secret Image and code were different" +``` + +## Contributing + +For testing, generate a temporary Rails dummy app inside test: + +```bash +bundle +bundle exec rake dummy:setup +bundle exec rake app:db:migrate +bundle exec rake app:db:migrate RAILS_ENV=test +bundle exec rake test +``` + +Please add test cases when adding new functionality. I started with some basic example integration tests for a very basic coverage. + +The tests will be run on [Travis-CI](https://travis-ci.org/pludoni/simple-captcha). + +## Who's who? + +Enjoy the simplest captcha implementation. + +Original Author of the Version for Rails 2: +Author: Sur, Blog: http://expressica.com, Contact: sur.max@gmail.com +Plugin Homepage: http://expressica.com/simple_captcha + +Plugin update for rails 3: http://github.com/galetahub + +update for Rails 4, tests and forked by Stefan Wienert (pludoni GmbH) diff --git a/README.rdoc b/README.rdoc deleted file mode 100644 index ad4ca02..0000000 --- a/README.rdoc +++ /dev/null @@ -1,231 +0,0 @@ -=SimpleCaptcha - -SimpleCaptcha is the simplest and a robust captcha plugin. Its implementation requires -adding up a single line in views and in controllers/models. -SimpleCaptcha is available to be used with Rails 3 or above and also it provides the -backward compatibility with previous versions of Rails. - -==Features - -* Zero FileSystem usage(secret code moved to db-store and image storage removed). -* Provides various image styles. -* Provides three level of complexity of images. -* Works absolutely fine in distributed environment(session and db based implementation works fine in distributed environment). -* Implementation is as easy as just writing a single line in your view. "<%= show_simple_captcha %>" within the 'form' tags. -* Flexible DOM and CSS handling(There is a separate view partial for rednering SimpleCaptcha DOM elements). -* Automated removal of 1 hour old unmatched simple_captcha data. - -==Requirements - -* {Ruby}[http://ruby-lang.org/] >= 1.8.7 -* {Rails}[http://github.com/rails/rails] >= 3 -* ImageMagick should be installed on your machine to use this plugin. - visit http://www.imagemagick.org/script/index.php for more details. - -==Installation - - gem "galetahub-simple_captcha", :require => "simple_captcha" - -or - - gem 'simple_captcha', :git => 'git://github.com/galetahub/simple-captcha.git' - -==Setup - -After installation, follow these simple steps to setup the plugin. The setup will depend -on the version of rails your application is using. - - rails generate simple_captcha - - rake db:migrate - -==Usage - -===Controller Based - -Add the following line in the file "app/controllers/application.rb" - - ApplicationController < ActionController::Base - include SimpleCaptcha::ControllerHelpers - end - -In the view file within the form tags add this code - - <%= show_simple_captcha %> - -and in the controller's action authenticate it as - - if simple_captcha_valid? - do this - else - do that - end - -===Model Based - -In the view file within the form tags write this code - - <%= show_simple_captcha(:object=>"user") %> - -and in the model class add this code - - class User < ActiveRecord::Base - apply_simple_captcha - end - -====FormBuilder helper - - <%= form_for @user do |form| -%> - ... - <%= form.simple_captcha :label => "Enter numbers.." %> - ... - <% end -%> - -====Validating with captcha -NOTE: @user.valid? will still work as it should, it will not validate the captcha code. - - @user.valid_with_captcha? - -====Saving with captcha -NOTE: @user.save will still work as it should, it will not validate the captcha code. - - @user.save_with_captcha - -===Formtastic integration -SimpleCaptcha detects if your use Formtastic and appends "SimpleCaptcha::CustomFormBuilder". - - <%= form.input :captcha, :as => :simple_captcha %> - -==Options & Examples -===View Options - -* *label* - provides the custom text b/w the image and the text field, the default is "type the code from the image" - -* *object* - the name of the object of the model class, to implement the model based captcha. - -* *code_type* - return numeric only if set to 'numeric' - -===Global options - -* *image_style* - provides the specific image style for the captcha image. -There are eight different styles available with the plugin as... - 1) simply_blue - 2) simply_red - 3) simply_green - 4) charcoal_grey - 5) embosed_silver - 6) all_black - 7) distorted_black - 8) almost_invisible -Default style is 'simply_blue'. -You can also specify 'random' to select the random image style. - -* *distortion* - handles the complexity of the image. The :distortion can be set to 'low', 'medium' or 'high'. Default is 'low'. - -Create "rails_root/config/initializers/simple_captcha.rb" - - SimpleCaptcha.setup do |sc| - # default: 100x28 - sc.image_size = '120x40' - - # default: 5 - sc.length = 6 - - # default: simply_blue - # possible values: - # 'embosed_silver', - # 'simply_red', - # 'simply_green', - # 'simply_blue', - # 'distorted_black', - # 'all_black', - # 'charcoal_grey', - # 'almost_invisible' - # 'random' - sc.image_style = 'simply_green' - - # default: low - # possible values: 'low', 'medium', 'high', 'random' - sc.distortion = 'medium' - end - -You can add your own style: - - SimpleCaptcha.setup do |sc| - sc.image_style = 'mycaptha' - sc.add_image_style('mycaptha', [ - "-background '#F4F7F8'", - "-fill '#86818B'", - "-border 1", - "-bordercolor '#E0E2E3'"]) - end - -You can provide the path where image_magick is installed as well: - - SimpleCaptcha.setup do |sc| - sc.image_magick_path = '/usr/bin' # you can check this from console by running: which convert - end - -You can provide the path where should be stored tmp files. -It's usefull when you dont have acces to /tmp (default directory) - - SimpleCaptcha.setup do |sc| - sc.tmp_path = '/tmp' # or somewhere in project eg. Rails.root.join('tmp/simple_captcha').to_s, make shure directory exists - end - - -===How to change the CSS for SimpleCaptcha DOM elements? -You can change the CSS of the SimpleCaptcha DOM elements as per your need in this file. - /app/views/simple_captcha/_simple_captcha.erb - -===View's Examples -====Controller Based Example - - <%= show_simple_captcha %> - - <%= show_simple_captcha(:label => "human authentication") %> - -====Model Based Example - - <%= show_simple_captcha(:object => 'user', :label => "human authentication") %> - -====Model Options - -* *message* - provides the custom message on failure of captcha authentication the default is "Secret Code did not match with the Image" - -* *add_to_base* - if set to true, appends the error message to the base. - -=====Model's Example - - class User < ActiveRecord::Base - apply_simple_captcha - end - - class User < ActiveRecord::Base - apply_simple_captcha :message => "The secret Image and code were different", :add_to_base => true - end - -==I18n - - simple_captcha: - placeholder: "Enter the image value" - label: "Enter the code in the box:" - message: - default: "Secret Code did not match with the Image" - user: "The secret Image and code were different" - -==Who's who? - -Enjoy the simplest captcha implementation. - -Author: Sur - -Blog: http://expressica.com - -Contact: sur.max@gmail.com - -Plugin Homepage: http://expressica.com/simple_captcha - -Plugin update for rails 3: http://github.com/galetahub - -Any feedback/comment/issue/donation is welcome! diff --git a/Rakefile b/Rakefile index 7901e39..e0f5fa8 100644 --- a/Rakefile +++ b/Rakefile @@ -1,22 +1,69 @@ -require 'rake' -require 'rake/testtask' -require 'rake/rdoctask' +begin + require 'bundler/setup' +rescue LoadError + puts 'You must `gem install bundler` and `bundle install` to run rake tasks' +end + +require 'rdoc/task' + +RDoc::Task.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = 'SimpleCaptcha' + rdoc.options << '--line-numbers' + rdoc.rdoc_files.include('README.rdoc') + rdoc.rdoc_files.include('lib/**/*.rb') +end + +APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__) +if File.exist? APP_RAKEFILE + load 'rails/tasks/engine.rake' +end + +namespace :dummy do + desc 'Setup dummy Rails app for test purpose' + task :setup do + require 'rails' + require 'simple_captcha' + if ENV["SEQUEL"].nil? + require File.expand_path('../test/lib/generators/simple_captcha/dummy/dummy_generator', __FILE__) + generator_class = SimpleCaptcha::DummyGenerator + else + require File.expand_path('../test/lib/generators/simple_captcha/dummy/dummy_generator_sequel', __FILE__) + generator_class = SimpleCaptcha::DummyGeneratorSequel + end + generator_class.start %w(--quiet) + end -desc 'Default: run unit tests.' -task :default => :test + desc 'destroy dummy Rails app under test/dummy' + task :destroy do + FileUtils.rm_rf "test/dummy" if File.exist?("test/dummy") + end + + desc 'redo' + task :redo do + sh 'rake dummy:destroy' + sh 'rake dummy:setup' + sh 'rake app:db:migrate' + sh 'rake app:db:migrate RAILS_ENV=test' + end + + +end + + + + +Bundler::GemHelper.install_tasks :name => 'simple_captcha2' + +require 'rake/testtask' -desc 'Test the simple_captcha plugin.' Rake::TestTask.new(:test) do |t| t.libs << 'lib' + t.libs << 'test' t.pattern = 'test/**/*_test.rb' - t.verbose = true + t.verbose = false end -desc 'Generate documentation for the simple_captcha plugin.' -Rake::RDocTask.new(:rdoc) do |rdoc| - rdoc.rdoc_dir = 'rdoc' - rdoc.title = 'SimpleCaptcha' - rdoc.options << '--line-numbers' << '--inline-source' - rdoc.rdoc_files.include('README') - rdoc.rdoc_files.include('lib/**/*.rb') -end + +task default: :test + diff --git a/lib/generators/USAGE b/lib/generators/USAGE index b36fdc0..53c0e84 100644 --- a/lib/generators/USAGE +++ b/lib/generators/USAGE @@ -1,4 +1,7 @@ -SimplaCaptcha +SimpleCaptcha ============= # Generate migration and copy view partial - rails generate simple_captcha + rails generate simple_captcha [template_format] + template_format Sets template format to be generated + Available options: erb, haml + Default: erb diff --git a/lib/generators/simple_captcha_generator.rb b/lib/generators/simple_captcha_generator.rb index 89beb04..e826de3 100644 --- a/lib/generators/simple_captcha_generator.rb +++ b/lib/generators/simple_captcha_generator.rb @@ -1,8 +1,9 @@ require 'rails/generators' class SimpleCaptchaGenerator < Rails::Generators::Base + argument :template_format, :type => :string, :default => 'erb' include Rails::Generators::Migration - + def self.source_root @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates/')) end @@ -10,12 +11,19 @@ def self.source_root def self.next_migration_number(dirname) Time.now.strftime("%Y%m%d%H%M%S") end - + def create_partial - template "partial.erb", File.join('app/views', 'simple_captcha', "_simple_captcha.erb") + template "partial.#{template_format}", File.join('app/views', 'simple_captcha', "_simple_captcha.#{template_format}") + end + + def create_captcha_migration + migration_template migration_file, File.join('db/migrate', "create_simple_captcha_data.rb") end - - def create_migration - migration_template "migration.rb", File.join('db/migrate', "create_simple_captcha_data.rb") + + private + + def migration_file + return "migration_sequel.rb" if defined?(Sequel) + Rails::VERSION::MAJOR > 4 ? "migration5.rb" : "migration.rb" end end diff --git a/lib/generators/templates/migration5.rb b/lib/generators/templates/migration5.rb new file mode 100644 index 0000000..92605ca --- /dev/null +++ b/lib/generators/templates/migration5.rb @@ -0,0 +1,15 @@ +class CreateSimpleCaptchaData < ActiveRecord::Migration[4.2] + def self.up + create_table :simple_captcha_data do |t| + t.string :key, :limit => 40 + t.string :value, :limit => 6 + t.timestamps + end + + add_index :simple_captcha_data, :key, :name => "idx_key" + end + + def self.down + drop_table :simple_captcha_data + end +end diff --git a/lib/generators/templates/migration_sequel.rb b/lib/generators/templates/migration_sequel.rb new file mode 100644 index 0000000..23e73b4 --- /dev/null +++ b/lib/generators/templates/migration_sequel.rb @@ -0,0 +1,17 @@ +Sequel.migration do + up do + create_table :simple_captcha_data do + primary_key :id + String :key, size: 40 + String :value, size: 6 + DateTime :created_at + DateTime :updated_at + + index :key, name: "idx_key" + end + end + + down do + drop_table :simple_captcha_data + end +end diff --git a/lib/generators/templates/partial.erb b/lib/generators/templates/partial.erb index 3ad8590..6419e8f 100644 --- a/lib/generators/templates/partial.erb +++ b/lib/generators/templates/partial.erb @@ -1,4 +1,4 @@ - + +
+
+ <%= simple_captcha_options[:image] %> +
+ +
+ <%= simple_captcha_options[:field] %> +
+ +
+ <%= simple_captcha_options[:label] %> +
+ +
+ <%= simple_captcha_options[:refresh_button] %> +
+
diff --git a/test/lib/generators/simple_captcha/dummy/templates/config/boot.rb b/test/lib/generators/simple_captcha/dummy/templates/config/boot.rb new file mode 100644 index 0000000..eaa6ee8 --- /dev/null +++ b/test/lib/generators/simple_captcha/dummy/templates/config/boot.rb @@ -0,0 +1,4 @@ +# Set up gems listed in the Gemfile. +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__) + +require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) diff --git a/test/lib/generators/simple_captcha/dummy/templates/config/database.yml b/test/lib/generators/simple_captcha/dummy/templates/config/database.yml new file mode 100644 index 0000000..72a0482 --- /dev/null +++ b/test/lib/generators/simple_captcha/dummy/templates/config/database.yml @@ -0,0 +1,29 @@ + +sqlite: &sqlite + adapter: sqlite3 + database: db/<%= Rails.env %>.sqlite3 + +mysql: &mysql + adapter: mysql2 + username: root + password: + database: myapp_<%= Rails.env %> + +postgresql: &postgresql + adapter: postgresql + username: postgres + password: + database: myapp_<%= Rails.env %> + min_messages: ERROR + +defaults: &defaults + pool: 5 + timeout: 5000 + host: localhost + <<: *<%= ENV['DB'] || "sqlite" %> + +development: + <<: *defaults + +test: + <<: *defaults diff --git a/test/lib/generators/simple_captcha/dummy/templates/config/routes.rb b/test/lib/generators/simple_captcha/dummy/templates/config/routes.rb new file mode 100644 index 0000000..c77c724 --- /dev/null +++ b/test/lib/generators/simple_captcha/dummy/templates/config/routes.rb @@ -0,0 +1,8 @@ +Rails.application.routes.draw do + get "pages/form_tag" + post "pages/form_tag" => 'pages#form_tag_submit' + get "pages/model_tag" + post "pages/model_tag" => 'pages#model_tag_submit' + get "pages/formtastic_tag" + post "pages/formtastic_tag" => 'pages#formtastic_tag_submit' +end diff --git a/test/model.rb b/test/model.rb new file mode 100644 index 0000000..7c67ed9 --- /dev/null +++ b/test/model.rb @@ -0,0 +1,24 @@ +require 'simple_captcha' +if Rails.version > '4.0' + class User + include ActiveModel::Model + include SimpleCaptcha::ModelHelpers + apply_simple_captcha + end +else + class User + include ActiveModel::Validations + include ActiveModel::Conversion + extend ActiveModel::Naming + include SimpleCaptcha::ModelHelpers + include ActiveModel::MassAssignmentSecurity + apply_simple_captcha + attr_accessible :key + def persisted?; false; end + def initialize(params={}) + params.each do |attr, value| + self.public_send("#{attr}=", value) + end if params + end + end +end diff --git a/test/simple_captcha_test.rb b/test/simple_captcha_test.rb deleted file mode 100644 index ec88ad1..0000000 --- a/test/simple_captcha_test.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'test/unit' - -class SimpleCaptchaTest < Test::Unit::TestCase - # Replace this with your real tests. - def test_this_plugin - flunk - end -end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..4165c9f --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,24 @@ +# Configure Rails Environment +ENV["RAILS_ENV"] = "test" + +require File.expand_path("../dummy/config/environment.rb", __FILE__) +require "rails/test_help" + +require 'capybara/rails' +require 'pry' +require 'model' +Rails.backtrace_cleaner.remove_silencers! + +# Load support files +Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } + +require 'capybara/poltergeist' +Capybara.javascript_driver = :poltergeist + +if defined?(Sequel) + module ::SimpleCaptcha + def SimpleCaptchaData.delete_all + dataset.delete + end + end +end