Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I fix formtastic support and add alwais_check option #13

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/simple_captcha.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module SimpleCaptcha
autoload :ModelHelpers, 'simple_captcha/active_record'

autoload :FormBuilder, 'simple_captcha/form_builder'
autoload :CustomFormBuilder, 'simple_captcha/formtastic'
autoload :FormtasticFormBuilder, 'simple_captcha/formtastic'

autoload :SimpleCaptchaData, 'simple_captcha/simple_captcha_data'
autoload :Middleware, 'simple_captcha/middleware'
Expand Down
19 changes: 17 additions & 2 deletions lib/simple_captcha/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def apply_simple_captcha(options = {})
extend ClassMethods

attr_accessor :captcha, :captcha_key
attr_accessible :captcha, :captcha_key
end
end
end
Expand All @@ -64,8 +65,22 @@ def is_captcha_valid?
end
end

def save_with_captcha
valid_with_captcha? && save(:validate => false)
def save_with_captcha(options = {})
valid_with_captcha? && save({:validate => false, :without_capthca => true}.merge(options))
end

def save(options = {})
if options[:without_capthca]
super({:validate => false})# options.delete(:without_capthca)
else
if simple_captcha_options[:always_check] &&
( simple_captcha_options[:on].blank? ||
(simple_captcha_options[:on].include?(:create) && self.new_record?))
save_with_captcha(options)
else
super(options)
end
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_captcha/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Engine < ::Rails::Engine
ActionView::Helpers::FormBuilder.send(:include, SimpleCaptcha::FormBuilder)

if Object.const_defined?("Formtastic")
Formtastic::Helpers::FormHelper.builder = SimpleCaptcha::CustomFormBuilder
::Formtastic::SemanticFormBuilder.send :include, SimpleCaptcha::FormtasticFormBuilder
end
end

Expand Down
15 changes: 10 additions & 5 deletions lib/simple_captcha/formtastic.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
module SimpleCaptcha
class CustomFormBuilder < Formtastic::FormBuilder
module FormtasticFormBuilder
def self.included(base)
base.send(:include, InstanceMethods)
end

private
module InstanceMethods
include SimpleCaptcha::ViewHelper

def simple_captcha_input(method, options)
options.update :object => sanitized_object_name
self.send(:show_simple_captcha, options)
def simple_captcha_input(method, options)
options.update :object => sanitized_object_name
self.send(:show_simple_captcha, options)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/simple_captcha/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# encoding: utf-8
module SimpleCaptcha
VERSION = "0.1.2".freeze
VERSION = "0.1.6".freeze
end
2 changes: 1 addition & 1 deletion simple_captcha.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
require "simple_captcha/version"

Gem::Specification.new do |s|
s.name = "galetahub-simple_captcha"
s.name = "simple_captcha"
s.version = SimpleCaptcha::VERSION.dup
s.platform = Gem::Platform::RUBY
s.summary = "SimpleCaptcha is the simplest and a robust captcha plugin."
Expand Down