loyal_simple_captcha 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
data/Rakefile CHANGED
@@ -1,22 +1,2 @@
1
1
  require 'rake'
2
- require 'rake/testtask'
3
- require 'rake/rdoctask'
4
2
 
5
- desc 'Default: run unit tests.'
6
- task :default => :test
7
-
8
- desc 'Test the simple_captcha plugin.'
9
- Rake::TestTask.new(:test) do |t|
10
- t.libs << 'lib'
11
- t.pattern = 'test/**/*_test.rb'
12
- t.verbose = true
13
- end
14
-
15
- desc 'Generate documentation for the simple_captcha plugin.'
16
- Rake::RDocTask.new(:rdoc) do |rdoc|
17
- rdoc.rdoc_dir = 'rdoc'
18
- rdoc.title = 'SimpleCaptcha'
19
- rdoc.options << '--line-numbers' << '--inline-source'
20
- rdoc.rdoc_files.include('README')
21
- rdoc.rdoc_files.include('lib/**/*.rb')
22
- end
@@ -1,8 +1,9 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  require 'rails/generators'
2
3
 
3
4
  class SimpleCaptchaGenerator < Rails::Generators::Base
4
5
  include Rails::Generators::Migration
5
-
6
+
6
7
  def self.source_root
7
8
  @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates/'))
8
9
  end
@@ -10,11 +11,11 @@ class SimpleCaptchaGenerator < Rails::Generators::Base
10
11
  def self.next_migration_number(dirname)
11
12
  Time.now.strftime("%Y%m%d%H%M%S")
12
13
  end
13
-
14
+
14
15
  def create_partial
15
16
  template "partial.erb", File.join('app/views', 'simple_captcha', "_simple_captcha.erb")
16
17
  end
17
-
18
+
18
19
  def create_migration
19
20
  migration_template "migration.rb", File.join('db/migrate', "create_simple_captcha_data.rb")
20
21
  end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  class CreateSimpleCaptchaData < ActiveRecord::Migration
2
3
  def self.up
3
4
  create_table :simple_captcha_data do |t|
@@ -6,7 +7,7 @@ class CreateSimpleCaptchaData < ActiveRecord::Migration
6
7
  t.timestamps
7
8
  end
8
9
 
9
- add_index :simple_captcha_data, :key, :name => "idx_key"
10
+ add_index :simple_captcha_data, :key
10
11
  end
11
12
 
12
13
  def self.down
@@ -0,0 +1,2 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'simple_captcha'
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # -*- encoding : utf-8 -*-
2
2
 
3
3
  module SimpleCaptcha
4
4
  autoload :Utils, 'simple_captcha/utils'
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module SimpleCaptcha #:nodoc
2
3
  module ModelHelpers #:nodoc
3
4
  def self.included(base)
@@ -53,6 +54,10 @@ module SimpleCaptcha #:nodoc
53
54
  def valid_with_captcha?
54
55
  [valid?, is_captcha_valid?].all?
55
56
  end
57
+
58
+ def simple_captcha_valid?
59
+ self.errors[:captcha].empty?
60
+ end
56
61
 
57
62
  def is_captcha_valid?
58
63
  return true if Rails.env.test?
@@ -61,12 +66,20 @@ module SimpleCaptcha #:nodoc
61
66
  SimpleCaptcha::Utils::simple_captcha_passed!(captcha_key)
62
67
  return true
63
68
  else
64
- message = simple_captcha_options[:message] || I18n.t(self.class.model_name.downcase, :scope => [:simple_captcha, :message], :default => :default)
65
- simple_captcha_options[:add_to_base] ? errors.add(:base, message) : errors.add(:captcha, message)
69
+ message = simple_captcha_options[:message] || I18n.t(
70
+ self.class.model_name.downcase, :scope => [:simple_captcha, :message], :default => :default
71
+ )
72
+
73
+ # simple_captcha_options[:add_to_base] ? errors.add(:base, message) : errors.add(:captcha, message)
74
+ errors.add(:captcha, message)
66
75
  return false
67
76
  end
68
77
  end
69
-
78
+
79
+ def update_attributes_with_captcha *args
80
+ valid_with_captcha? && update_attributes(*args)
81
+ end
82
+
70
83
  def save_with_captcha
71
84
  valid_with_captcha? && save(:validate => false)
72
85
  end
@@ -1,28 +1,57 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module SimpleCaptcha #:nodoc
2
3
  module ControllerHelpers #:nodoc
3
- # This method is to validate the simple captcha in controller.
4
- # It means when the captcha is controller based i.e. :object has not been passed to the method show_simple_captcha.
5
- #
6
- # *Example*
7
- #
8
- # If you want to save an object say @user only if the captcha is validated then do like this in action...
9
- #
10
- # if simple_captcha_valid?
11
- # @user.save
12
- # else
13
- # flash[:notice] = "captcha did not match"
14
- # redirect_to :action => "myaction"
15
- # end
16
- def simple_captcha_valid?
17
- return true if Rails.env.test?
18
-
19
- if params[:captcha]
20
- data = SimpleCaptcha::Utils::simple_captcha_value(params[:captcha_key] || session[:captcha])
21
- result = data == params[:captcha].delete(" ").upcase
22
- SimpleCaptcha::Utils::simple_captcha_passed!(session[:captcha]) if result
23
- return result
24
- else
25
- return false
4
+ def self.included base
5
+ base.class_eval do
6
+ include InstanceMethods
7
+ helper_method :generate_simple_captcha_options
8
+ end
9
+ end
10
+
11
+ module InstanceMethods
12
+ # This method is to validate the simple captcha in controller.
13
+ # It means when the captcha is controller based i.e. :object has not been passed to the method show_simple_captcha.
14
+ #
15
+ # *Example*
16
+ #
17
+ # If you want to save an object say @user only if the captcha is validated then do like this in action...
18
+ #
19
+ # if simple_captcha_valid?
20
+ # @user.save
21
+ # else
22
+ # flash[:notice] = "captcha did not match"
23
+ # redirect_to :action => "myaction"
24
+ # end
25
+ def simple_captcha_valid?
26
+ return true if Rails.env.test?
27
+
28
+ if params[:captcha]
29
+ data = SimpleCaptcha::Utils::simple_captcha_value(params[:captcha_key] || session[:captcha])
30
+ result = data == params[:captcha].delete(" ").upcase
31
+ SimpleCaptcha::Utils::simple_captcha_passed!(session[:captcha]) if result
32
+ return result
33
+ else
34
+ return false
35
+ end
36
+ end
37
+
38
+ # 生成验证的信息
39
+ # options:
40
+ # - object
41
+ def generate_simple_captcha_options *args
42
+ options = args.extract_options!
43
+
44
+ key = SimpleCaptcha::Utils.generate_key_use_session(
45
+ session, options[:object]
46
+ )
47
+
48
+ {
49
+ :image_url => ::SimpleCaptcha::Utils.generate_image_url(key, request.base_url, options),
50
+ :label => options[:label] || I18n.t('simple_captcha.label'),
51
+ :captcha_key => ::SimpleCaptcha::Utils.set_simple_captcha_data(key, options),
52
+ :object => options[:object]
53
+ }
54
+
26
55
  end
27
56
  end
28
57
  end
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # -*- encoding : utf-8 -*-
2
2
  require 'rails'
3
3
  require 'simple_captcha'
4
4
 
@@ -14,6 +14,8 @@ module SimpleCaptcha
14
14
  ActionView::Base.send(:include, SimpleCaptcha::ViewHelper)
15
15
  ActionView::Helpers::FormBuilder.send(:include, SimpleCaptcha::FormBuilder)
16
16
 
17
+ ActionController::Base.send :include, ::SimpleCaptcha::ControllerHelpers
18
+
17
19
  if Object.const_defined?("Formtastic")
18
20
  if Formtastic.const_defined?("Helpers")
19
21
  Formtastic::Helpers::FormHelper.builder = SimpleCaptcha::CustomFormBuilder
@@ -1,11 +1,12 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module SimpleCaptcha
2
3
  module FormBuilder
3
4
  def self.included(base)
4
5
  base.send(:include, SimpleCaptcha::ViewHelper)
5
6
  base.send(:include, SimpleCaptcha::FormBuilder::ClassMethods)
6
7
  base.send(:include, ActionView::Helpers)
7
- base.send(:include, Sprockets::Helpers::RailsHelper)
8
- base.send(:include, Sprockets::Helpers::IsolatedHelper)
8
+ # base.send(:include, Sprockets::Helpers::RailsHelper)
9
+ # base.send(:include, Sprockets::Helpers::IsolatedHelper)
9
10
 
10
11
  base.delegate :render, :session, :to => :template
11
12
  end
@@ -34,7 +35,7 @@ module SimpleCaptcha
34
35
  html[:placeholder] = options[:placeholder] || I18n.t('simple_captcha.placeholder')
35
36
 
36
37
  text_field(:captcha, html) +
37
- hidden_field(:captcha_key, {:value => options[:field_value]})
38
+ hidden_field(:captcha_key, {:value => options[:captcha_key], :class => 'captcha-key'})
38
39
  end
39
40
  end
40
41
  end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module SimpleCaptcha
2
3
  class CustomFormBuilder < Formtastic::FormBuilder
3
4
 
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  require 'tempfile'
2
3
  module SimpleCaptcha #:nodoc
3
4
  module ImageHelpers #:nodoc
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # -*- encoding : utf-8 -*-
2
2
  module SimpleCaptcha
3
3
  class Middleware
4
4
  include SimpleCaptcha::ImageHelpers
@@ -1,10 +1,15 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module SimpleCaptcha
2
3
  class SimpleCaptchaData < ::ActiveRecord::Base
3
4
  def self.rails3?
4
5
  ::ActiveRecord::VERSION::MAJOR == 3
5
6
  end
6
7
 
7
- if rails3?
8
+ def self.rails4?
9
+ ::ActiveRecord::VERSION::MAJOR == 4
10
+ end
11
+
12
+ if rails3? || rails4?
8
13
  # Fixes deprecation warning in Rails 3.2:
9
14
  # DEPRECATION WARNING: Calling set_table_name is deprecated. Please use `self.table_name = 'the_name'` instead.
10
15
  self.table_name = "simple_captcha_data"
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module SimpleCaptcha
2
3
  class SimpleCaptchaData
3
4
  include Mongoid::Document
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  require 'digest/sha1'
2
3
 
3
4
  module SimpleCaptcha #:nodoc
@@ -32,5 +33,50 @@ module SimpleCaptcha #:nodoc
32
33
  args << Time.now.to_s
33
34
  Digest::SHA1.hexdigest(args.join)
34
35
  end
36
+
37
+ def self.set_simple_captcha_data(key, options={})
38
+ code_type = options[:code_type]
39
+
40
+ data = SimpleCaptcha::SimpleCaptchaData.get_data(key)
41
+ data.value = generate_simple_captcha_data(code_type)
42
+ data.save
43
+ key
44
+ end
45
+
46
+ def self.generate_simple_captcha_data(code)
47
+ value = ''
48
+
49
+ case code
50
+ when 'numeric' then
51
+ SimpleCaptcha.length.times{value << (48 + rand(10)).chr}
52
+ else
53
+ SimpleCaptcha.length.times{value << (65 + rand(26)).chr}
54
+ end
55
+
56
+ return value
57
+ end
58
+
59
+
60
+ def self.generate_image_url key, *args
61
+ options = args.extract_options!
62
+
63
+ defaults = {}
64
+ defaults[:time] = options[:time] || Time.now.to_i
65
+
66
+ base_url = args.first || ENV['RAILS_RELATIVE_URL_ROOT']
67
+
68
+ url = "#{base_url}/simple_captcha?code=#{key}&#{defaults.to_query}"
69
+ end
70
+
71
+ def self.generate_key_use_session session, *args
72
+ options = args.extract_options!
73
+ key_name = args.first
74
+
75
+ if key_name.nil?
76
+ session[:captcha] ||= SimpleCaptcha::Utils.generate_key(session[:id].to_s, 'captcha')
77
+ else
78
+ SimpleCaptcha::Utils.generate_key(session[:id].to_s, key_name)
79
+ end
80
+ end
35
81
  end
36
82
  end
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # -*- encoding : utf-8 -*-
2
2
  module SimpleCaptcha
3
- VERSION = "0.0.1".freeze
3
+ VERSION = "0.0.3".freeze
4
4
  end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module SimpleCaptcha #:nodoc
2
3
  module ViewHelper #:nodoc
3
4
 
@@ -42,29 +43,35 @@ module SimpleCaptcha #:nodoc
42
43
  # Find more detailed examples with sample images here on my blog http://EXPRESSICA.com
43
44
  #
44
45
  # All Feedbacks/CommentS/Issues/Queries are welcome.
45
- def show_simple_captcha(options={})
46
- key = simple_captcha_key(options[:object])
47
- options[:field_value] = set_simple_captcha_data(key, options)
46
+ #
47
+ #
48
+ # eg. show_simple_captcha :label => '', :captcha_key => '', :image_url => ''
49
+ def show_simple_captcha(*args)
50
+ options = args.extract_options!
51
+
52
+ options[:captcha_key] ||= set_simple_captcha_data(
53
+ (
54
+ args.first || simple_captcha_key(options[:object])
55
+ ),
56
+ options
57
+ )
58
+
59
+ key = options[:captcha_key]
48
60
 
49
61
  defaults = {
50
- :image => simple_captcha_image(key, options),
51
- :label => options[:label] || I18n.t('simple_captcha.label'),
52
- :field => simple_captcha_field(options)
53
- }
54
-
62
+ :image => simple_captcha_image(key, options),
63
+ :label => options[:label] || I18n.t('simple_captcha.label'),
64
+ :field => simple_captcha_field(options)
65
+ }
66
+
55
67
  render :partial => 'simple_captcha/simple_captcha', :locals => { :simple_captcha_options => defaults }
56
68
  end
57
69
 
58
70
  private
59
71
 
60
- def simple_captcha_image(simple_captcha_key, options = {})
61
- defaults = {}
62
- defaults[:time] = options[:time] || Time.now.to_i
63
-
64
- query = defaults.collect{ |key, value| "#{key}=#{value}" }.join('&')
65
- url = "#{ENV['RAILS_RELATIVE_URL_ROOT']}/simple_captcha?code=#{simple_captcha_key}&#{query}"
66
-
67
- tag('img', :src => url, :alt => 'captcha')
72
+ def simple_captcha_image(key, options = {})
73
+ url = options[:image_url] || ::SimpleCaptcha::Utils.generate_image_url(key, options)
74
+ tag('img', :src => url, :alt => 'captcha', :class => 'captcha-image')
68
75
  end
69
76
 
70
77
  def simple_captcha_field(options={})
@@ -74,42 +81,19 @@ module SimpleCaptcha #:nodoc
74
81
 
75
82
  if options[:object]
76
83
  text_field(options[:object], :captcha, html.merge(:value => '')) +
77
- hidden_field(options[:object], :captcha_key, {:value => options[:field_value]})
84
+ hidden_field(options[:object], :captcha_key, {:value => options[:captcha_key], :class => 'captcha-key'})
78
85
  else
79
86
  text_field_tag(:captcha, nil, html) +
80
- hidden_field_tag(:captcha_key, options[:field_value])
87
+ hidden_field_tag(:captcha_key, options[:captcha_key], :class => 'captcha-key')
81
88
  end
82
89
  end
83
90
 
84
91
  def set_simple_captcha_data(key, options={})
85
- code_type = options[:code_type]
86
-
87
- value = generate_simple_captcha_data(code_type)
88
- data = SimpleCaptcha::SimpleCaptchaData.get_data(key)
89
- data.value = value
90
- data.save
91
- key
92
+ ::SimpleCaptcha::Utils.set_simple_captcha_data key, options
92
93
  end
93
-
94
- def generate_simple_captcha_data(code)
95
- value = ''
96
-
97
- case code
98
- when 'numeric' then
99
- SimpleCaptcha.length.times{value << (48 + rand(10)).chr}
100
- else
101
- SimpleCaptcha.length.times{value << (65 + rand(26)).chr}
102
- end
103
-
104
- return value
105
- end
106
-
94
+
107
95
  def simple_captcha_key(key_name = nil)
108
- if key_name.nil?
109
- session[:captcha] ||= SimpleCaptcha::Utils.generate_key(session[:id].to_s, 'captcha')
110
- else
111
- SimpleCaptcha::Utils.generate_key(session[:id].to_s, key_name)
112
- end
96
+ SimpleCaptcha::Utils.generate_key_use_session session, key_name
113
97
  end
114
98
  end
115
99
  end
metadata CHANGED
@@ -1,24 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loyal_simple_captcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - Pavlo Galeta
9
- - Igor Galeta
8
+ - happy
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2013-03-26 00:00:00.000000000 Z
12
+ date: 2013-08-03 00:00:00.000000000 Z
14
13
  dependencies: []
15
- description: SimpleCaptcha is available to be used with Rails 3 or above and also
16
- it provides the backward compatibility with previous versions of Rails.
17
- email: galeta.igor@gmail.com
14
+ description: LoyalSimpleCaptcha is available to be used with Rails 3 or above and
15
+ also it provides the backward compatibility with previous versions of Rails.
16
+ email: andywang7259@gmail.com
18
17
  executables: []
19
18
  extensions: []
20
- extra_rdoc_files:
21
- - README.rdoc
19
+ extra_rdoc_files: []
22
20
  files:
23
21
  - lib/simple_captcha.rb
24
22
  - lib/simple_captcha/view.rb
@@ -33,13 +31,14 @@ files:
33
31
  - lib/simple_captcha/controller.rb
34
32
  - lib/simple_captcha/image.rb
35
33
  - lib/simple_captcha/middleware.rb
34
+ - lib/loyal_simple_captcha.rb
36
35
  - lib/generators/USAGE
37
36
  - lib/generators/simple_captcha_generator.rb
38
37
  - lib/generators/templates/partial.erb
39
38
  - lib/generators/templates/migration.rb
40
39
  - Rakefile
41
- - README.rdoc
42
- homepage: http://github.com/izzm/simple-captcha
40
+ - README.md
41
+ homepage: http://github.com/xiuxian123/loyals
43
42
  licenses: []
44
43
  post_install_message:
45
44
  rdoc_options: []