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

Support Sequel ORM #63

Merged
merged 3 commits into from
Dec 6, 2018
Merged
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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'
Expand Down
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
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'
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ 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
Expand Down
10 changes: 8 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ namespace :dummy do
task :setup do
require 'rails'
require 'simple_captcha'
require File.expand_path('../test/lib/generators/simple_captcha/dummy/dummy_generator', __FILE__)
SimpleCaptcha::DummyGenerator.start %w(--quiet)
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 'destroy dummy Rails app under test/dummy'
Expand Down
1 change: 1 addition & 0 deletions lib/generators/simple_captcha_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def create_captcha_migration
private

def migration_file
return "migration_sequel.rb" if defined?(Sequel)
Rails::VERSION::MAJOR > 4 ? "migration5.rb" : "migration.rb"
end
end
17 changes: 17 additions & 0 deletions lib/generators/templates/migration_sequel.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions lib/simple_captcha.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class << self

if defined?(ActiveRecord)
autoload :SimpleCaptchaData, 'simple_captcha/simple_captcha_data'
elsif defined?(Sequel)
autoload :SimpleCaptchaData, 'simple_captcha/simple_captcha_data_sequel'
else
autoload :SimpleCaptchaData, 'simple_captcha/simple_captcha_data_mongoid.rb'
end
Expand Down
22 changes: 22 additions & 0 deletions lib/simple_captcha/simple_captcha_data_sequel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module SimpleCaptcha
class SimpleCaptchaData < Sequel::Model
plugin :update_or_create
plugin :timestamps, update_on_create: true

class << self
def get_data(key)
find_or_new(key: key)
end

def remove_data(key)
where(key: key).delete
clear_old_data(1.hour.ago)
end

def clear_old_data(time = 1.hour.ago)
return unless Time === time
where {updated_at < time}.delete
end
end
end
end
4 changes: 2 additions & 2 deletions test/features/form_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

class FormHelperTest < ActionDispatch::IntegrationTest
include Capybara::DSL
self.use_transactional_tests = false if Rails::VERSION::MAJOR >= 5
self.use_transactional_fixtures = false if Rails::VERSION::MAJOR < 5
self.use_transactional_tests = false if Rails::VERSION::MAJOR >= 5 && !defined?(Sequel)
self.use_transactional_fixtures = false if Rails::VERSION::MAJOR < 5 && !defined?(Sequel)

setup do
SimpleCaptcha.always_pass = false
Expand Down
4 changes: 2 additions & 2 deletions test/features/formtastic_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
if defined? Formtastic
require 'test_helper'
class FormtasticTest < ActionDispatch::IntegrationTest
self.use_transactional_tests = false if Rails::VERSION::MAJOR >= 5
self.use_transactional_fixtures = false if Rails::VERSION::MAJOR < 5
self.use_transactional_tests = false if Rails::VERSION::MAJOR >= 5 && !defined?(Sequel)
self.use_transactional_fixtures = false if Rails::VERSION::MAJOR < 5 && !defined?(Sequel)
include Capybara::DSL

setup do
Expand Down
22 changes: 22 additions & 0 deletions test/lib/generators/simple_captcha/dummy/dummy_generator_sequel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require File.expand_path('../dummy_generator', __FILE__)

module SimpleCaptcha
class DummyGeneratorSequel < DummyGenerator
desc "Creates dummy test Rails application (sequel version)"

def generate_test_dummy
opts = (options || {}).slice(*PASSTHROUGH_OPTIONS) || {} # rails 3.2 ? -> slice is nil
opts[:force] = true
opts[:skip_bundle] = true
opts[:old_style_hash] = true
opts[:skip_active_record] = true unless ENV["SEQUEL"].nil?

copy_file "config/database.yml", "#{dummy_path}/config/database.yml", :force => true

invoke Rails::Generators::AppGenerator, [ File.expand_path(dummy_path, destination_root) ], opts
create_file "#{dummy_path}/config/initializers/sequel.rb", "Rails.application.config.sequel.schema_dump = false"
run "mkdir db"
run "rails generate simple_captcha"
end
end
end
8 changes: 8 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@

require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist

if defined?(Sequel)
module ::SimpleCaptcha
def SimpleCaptchaData.delete_all
dataset.delete
end
end
end