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

Testing against latest Rails versions #1

Open
wants to merge 7 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
52 changes: 52 additions & 0 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Verify
on: [push]

jobs:
tests:
name: Tests
runs-on: ubuntu-latest

strategy:
matrix:
gemfile:
- rails-6.0.x
- rails-6.1.x
- rails-edge
ruby-version:
- 2.6
- 2.7
- head
exclude:
# activesupport-7.0.0.alpha requires ruby version >= 2.7.0, which is incompatible
- gemfile: rails-edge
ruby-version: 2.6

continue-on-error: ${{ endsWith(matrix.gemfile, 'edge') || matrix.ruby-version == 'head' }}

env:
RAILS_ENV: test
DATABASE_URL: postgresql://test:test@localhost/hstore_translate_test
BUNDLE_GEMFILE: test/gemfiles/${{ matrix.gemfile }}.gemfile

services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: test
POSTGRES_USER: test
POSTGRES_DB: hstore_translate_test
ports:
- 5432:5432

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup Ruby ${{ matrix.ruby-version }} and install ${{ matrix.gemfile }} gems
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
ruby-version: ${{ matrix.ruby-version }}

- name: Run tests
run: bundle exec rake
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
Gemfile.lock
pkg/*
test/coverage
.idea/
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

4 changes: 3 additions & 1 deletion lib/hstore_translate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ def self.native_hstore?

require 'activerecord-postgres-hstore' unless HstoreTranslate::native_hstore?

ActiveRecord::Base.extend(HstoreTranslate::Translates)
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.extend(HstoreTranslate::Translates)
end
45 changes: 25 additions & 20 deletions lib/hstore_translate/translates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ def translates(*attrs)
end
end

alias_method_chain :respond_to?, :translates
alias_method_chain :method_missing, :translates
alias_method :respond_to_without_translates?, :respond_to?
alias_method :respond_to?, :respond_to_with_translates?
protected :respond_to_with_translates?

alias_method :method_missing_without_translates, :method_missing
alias_method :method_missing, :method_missing_with_translates
protected :method_missing_with_translates
end

# Improve compatibility with the gem globalize
Expand All @@ -44,10 +49,27 @@ def enable_fallback(&block)
toggle_fallback(enabled = true, &block)
end

def respond_to_with_translates?(symbol, include_all = false)
return true if parse_translated_attribute_accessor(symbol)
respond_to_without_translates?(symbol, include_all)
end

def method_missing_with_translates(method_name, *args)
translated_attr_name, locale, assigning = parse_translated_attribute_accessor(method_name)

return method_missing_without_translates(method_name, *args) unless translated_attr_name

if assigning
write_hstore_translation(translated_attr_name, args.first, locale)
else
read_hstore_translation(translated_attr_name, locale)
end
end

protected

def hstore_translate_fallback_locales(locale)
return if @enabled_fallback == false || !I18n.respond_to?(:fallbacks)
return if (defined?(@enabled_fallback) && @enabled_fallback == false) || !I18n.respond_to?(:fallbacks)
I18n.fallbacks[locale]
end

Expand Down Expand Up @@ -77,23 +99,6 @@ def write_hstore_translation(attr_name, value, locale = I18n.locale)
value
end

def respond_to_with_translates?(symbol, include_all = false)
return true if parse_translated_attribute_accessor(symbol)
respond_to_without_translates?(symbol, include_all)
end

def method_missing_with_translates(method_name, *args)
translated_attr_name, locale, assigning = parse_translated_attribute_accessor(method_name)

return method_missing_without_translates(method_name, *args) unless translated_attr_name

if assigning
write_hstore_translation(translated_attr_name, args.first, locale)
else
read_hstore_translation(translated_attr_name, locale)
end
end

# Internal: Parse a translated convenience accessor name.
#
# method_name - The accessor name.
Expand Down
6 changes: 0 additions & 6 deletions test/gemfiles/Gemfile.rails-3.1.x

This file was deleted.

6 changes: 0 additions & 6 deletions test/gemfiles/Gemfile.rails-3.2.x

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'
gemspec :path => './../..'

gem 'activerecord', '~> 4.1.0'
gem 'activerecord', '~> 6.0.0'
gem 'pg', :platform => :ruby
gem 'activerecord-jdbcpostgresql-adapter', :platform => :jruby
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'
gemspec :path => './../..'

gem 'activerecord', '~> 4.0.0'
gem 'activerecord', '~> 6.1.0'
gem 'pg', :platform => :ruby
gem 'activerecord-jdbcpostgresql-adapter', :platform => :jruby
9 changes: 9 additions & 0 deletions test/gemfiles/rails-edge.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source 'https://rubygems.org'
gemspec :path => './../..'

git 'https://github.com/rails/rails.git' do
gem 'activerecord'
end

gem 'pg', :platform => :ruby
gem 'activerecord-jdbcpostgresql-adapter', :platform => :jruby
13 changes: 11 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def prepare_database

def db_config
@db_config ||= begin
return ENV['DATABASE_URL'] if ENV['DATABASE_URL']

filepath = File.join('test', 'database.yml')
YAML.load_file(filepath)['test']
end
Expand All @@ -32,9 +34,16 @@ def establish_connection(config)
end

def create_database
system_config = db_config.merge('database' => 'postgres', 'schema_search_path' => 'public')
# Using default postgres database to connect to create test database
system_config =
if db_config.is_a?(String)
db_config.gsub("/hstore_translate_test", "/postgres")
else
db_config.merge('database' => 'postgres', 'schema_search_path' => 'public')
end

connection = establish_connection(system_config)
connection.create_database(db_config['database']) rescue nil
connection.create_database("hstore_translate_test") rescue nil
enable_extension
end

Expand Down
9 changes: 8 additions & 1 deletion test/translates_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def test_retrieves_in_current_locale
def test_retrieves_in_current_locale_with_fallbacks
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
I18n.default_locale = :"en-US"
I18n.fallbacks = [I18n.default_locale]

p = Post.new(:title_translations => {"en" => "English Title"})
I18n.with_locale(:fr) do
Expand Down Expand Up @@ -53,6 +54,7 @@ def test_retrieves_in_specified_locale
def test_retrieves_in_specified_locale_with_fallbacks
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
I18n.default_locale = :"en-US"
I18n.fallbacks = [I18n.default_locale]

p = Post.new(:title_translations => { "en" => "English Title" })
I18n.with_locale(:fr) do
Expand All @@ -63,6 +65,7 @@ def test_retrieves_in_specified_locale_with_fallbacks
def test_fallback_from_empty_string
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
I18n.default_locale = :"en-US"
I18n.fallbacks = [I18n.default_locale]

p = Post.new(:title_translations => { "en" => "English Title", "fr" => "" })
I18n.with_locale(:fr) do
Expand All @@ -73,17 +76,19 @@ def test_fallback_from_empty_string
def test_retrieves_in_specified_locale_with_fallback_disabled
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
I18n.default_locale = :"en-US"
I18n.fallbacks = [I18n.default_locale]

p = Post.new(:title_translations => { "en" => "English Title" })
p.disable_fallback
I18n.with_locale(:fr) do
assert_equal(nil, p.title_fr)
assert_nil(p.title_fr)
end
end

def test_retrieves_in_specified_locale_with_fallback_disabled_using_a_block
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
I18n.default_locale = :"en-US"
I18n.fallbacks = [I18n.default_locale]

p = Post.new(:title_translations => { "en" => "English Title" })
p.enable_fallback
Expand All @@ -95,6 +100,7 @@ def test_retrieves_in_specified_locale_with_fallback_disabled_using_a_block
def test_retrieves_in_specified_locale_with_fallback_reenabled
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
I18n.default_locale = :"en-US"
I18n.fallbacks = [I18n.default_locale]

p = Post.new(:title_translations => { "en" => "English Title" })
p.disable_fallback
Expand All @@ -107,6 +113,7 @@ def test_retrieves_in_specified_locale_with_fallback_reenabled
def test_retrieves_in_specified_locale_with_fallback_reenabled_using_a_block
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
I18n.default_locale = :"en-US"
I18n.fallbacks = [I18n.default_locale]

p = Post.new(:title_translations => { "en" => "English Title" })
p.disable_fallback
Expand Down