Skip to content

Commit

Permalink
Merge pull request #362 from cubesystems/rails61
Browse files Browse the repository at this point in the history
Rails 6.1, Ruby 2.7 support
  • Loading branch information
miks committed Jan 24, 2021
2 parents e2a7463 + ad0ff2f commit dba2e47
Show file tree
Hide file tree
Showing 30 changed files with 188 additions and 249 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dist: bionic
language: ruby
cache: bundler
rvm:
- 2.6
- 2.7

addons:
apt:
Expand Down
2 changes: 1 addition & 1 deletion .versions.conf
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby=ruby-2.6
ruby=ruby-2.7
2 changes: 1 addition & 1 deletion lib/releaf/rspec/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def download_file(url)
require "open-uri"
file = Tempfile.new
file.binmode
file.write(open(url).read)
file.write(URI.open(url).read)
file.flush
file
end
Expand Down
6 changes: 4 additions & 2 deletions releaf-content/spec/features/nodes_services_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
expect( e.record ).to eq @text_page_node_3
end

expect(@text_page_node_3.errors.messages).to eq(name: [], base: ["descendant invalid"])
expect(@text_page_node_3).to have_error_on(:base, "descendant invalid")
expect(@text_page_node_3).to_not have_error_on(:name)
end
end

Expand Down Expand Up @@ -95,7 +96,8 @@
rescue ActiveRecord::RecordInvalid => e
expect( e.record ).to eq @text_page_node_3
end
expect(@text_page_node_3.errors.messages).to eq(name: [], base: ["descendant invalid"])
expect(@text_page_node_3).to have_error_on(:base, "descendant invalid")
expect(@text_page_node_3).to_not have_error_on(:name)
end

it "doesn't create any new nodes" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ContactFormController < ActionController::Base
end

it "has hard typed configuration options" do
expect{ Book.acts_as_node xxxx: ["x"] }.to raise_error(ArgumentError, "unknown keyword: xxxx")
expect{ Book.acts_as_node xxxx: ["x"] }.to raise_error(ArgumentError, "unknown keyword: :xxxx")
end
end

Expand Down
4 changes: 2 additions & 2 deletions releaf-content/spec/lib/releaf/content/route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,14 @@

context "when database doesn't exists" do
it "returns an empty array" do
allow(Node).to receive(:where).and_raise(ActiveRecord::NoDatabaseError.new("xxx"))
allow(Releaf::Content::BuildRouteObjects).to receive(:call).and_raise(ActiveRecord::NoDatabaseError.new("xxx"))
expect(described_class.for(Node, HomePage, 'foo')).to eq([])
end
end

context "when node table doesn't exist" do
it "returns an empty array" do
allow(Node).to receive(:where).and_raise(ActiveRecord::StatementInvalid.new("xxx"))
allow(Releaf::Content::BuildRouteObjects).to receive(:call).and_raise(ActiveRecord::StatementInvalid.new("xxx"))
expect(described_class.for(Node, HomePage, 'foo')).to eq([])
end
end
Expand Down
2 changes: 1 addition & 1 deletion releaf-core/app/builders/releaf/builders/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def safe_join

def t(key, options = {})
options[:scope] = default_translation_scope unless options.key? :scope
I18n.t(key, options)
I18n.t(key, **options)
end

def translate_locale(locale)
Expand Down
4 changes: 2 additions & 2 deletions releaf-core/app/helpers/releaf/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def releaf_table(collection, resource_class, options = {})
def translate(key, options = {})
# prevent I18n from raising errors when translation is missing
options.merge!(raise: false) unless options.key?(:raise)
super(key, options)
super(key, **options)
end
alias :t :translate

Expand All @@ -30,7 +30,7 @@ def i18n_options_for_select container, selected, prefix, i18n_options={}

container.each do|element|
text, value = i18n_option_text_and_value(element).map { |item| item.to_s }
text = I18n.t("#{prefix}-#{text}", i18n_options.merge(default: text))
text = I18n.t("#{prefix}-#{text}", **i18n_options.merge(default: text))
translated_container << [text, value]
end

Expand Down
7 changes: 1 addition & 6 deletions releaf-core/app/lib/releaf/action_controller/features.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Releaf::ActionController::Features
included do
before_action :verify_feature_availability!
helper_method :feature_available?
rescue_from Releaf::FeatureDisabled, with: :feature_disabled
rescue_from Releaf::FeatureDisabled, with: :access_denied
end

def verify_feature_availability!
Expand Down Expand Up @@ -33,11 +33,6 @@ def action_features
}
end

def feature_disabled(exception)
@feature = exception.message
respond_with(nil, responder: action_responder(:feature_disabled))
end

def feature_available?(feature)
return false if feature.blank?
return false if feature == :create_another && !feature_available?(:create)
Expand Down
24 changes: 12 additions & 12 deletions releaf-core/app/lib/releaf/build_errors_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ def call
errors.inject({}) do |h, item|
field_name = item.delete(:field_name)
h[field_name] ||= []
h[field_name] << item
# filter out nested item duplicate errors due to #nested_attribute_errors functionality
# that returns all errors on each objects
h[field_name] << item if h[field_name].none?{|error| error == item}
h
end
end

def errors
resource.errors.map do |attribute, message|
format_error(attribute, message)
end.flatten
resource.errors.map{|error| format_error(error) }.flatten
end

def attribute_error(attribute, message)
def attribute_error(error)
{
field_name: field_name(attribute),
error_code: message.error_code,
message: message.to_s,
field_name: field_name(error.attribute),
error_code: error.type,
message: error.message.to_s,
}
end

def format_error(attribute, message)
if resource_attribute?(attribute)
attribute_error(attribute, message)
def format_error(error)
if resource_attribute?(error.attribute)
attribute_error(error)
else
nested_attribute_errors(attribute)
nested_attribute_errors(error.attribute)
end
end

Expand Down
1 change: 0 additions & 1 deletion releaf-core/app/lib/releaf/responders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def action_responders
confirm_destroy: Releaf::Responders::ConfirmDestroyResponder,
destroy: Releaf::Responders::DestroyResponder,
access_denied: Releaf::Responders::AccessDeniedResponder,
feature_disabled: Releaf::Responders::FeatureDisabledResponder,
page_not_found: Releaf::Responders::PageNotFoundResponder,
}
end
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
= render 'releaf/error_pages/error', message: t('You are not authorized to access %{controller}', controller: controller_name, default: 'You are not authorized to access %{controller}', scope: controller_scope_name)
= render 'releaf/error_pages/error', message: t('You are not authorized to view this page', scope: controller_scope_name)

This file was deleted.

3 changes: 1 addition & 2 deletions releaf-core/lib/releaf-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
require 'dragonfly'
require 'globalize'
require 'virtus'
require 'globalize-accessors'

module Releaf
module Core
Expand All @@ -30,7 +29,7 @@ module Core
require 'releaf/route_mapper'
require 'releaf/exceptions'
require 'releaf/core_ext/array/reorder'
require 'releaf/rails_ext/validation_error_codes'
require 'releaf/rails_ext/globalize-accessors'

def self.components
[Releaf::SettingsUI, Releaf::Root]
Expand Down
64 changes: 64 additions & 0 deletions releaf-core/lib/releaf/rails_ext/globalize-accessors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'globalize'

module Globalize::Accessors
def globalize_accessors(options = {})
options.reverse_merge!(:locales => I18n.available_locales, :attributes => translated_attribute_names)
class_attribute :globalize_locales, :globalize_attribute_names, :instance_writer => false

self.globalize_locales = options[:locales]
self.globalize_attribute_names = []

each_attribute_and_locale(options) do |attr_name, locale|
define_accessors(attr_name, locale)
end

include InstanceMethods
end

def localized_attr_name_for(attr_name, locale)
"#{attr_name}_#{locale.to_s.underscore}"
end

private

def define_accessors(attr_name, locale)
define_getter(attr_name, locale)
define_setter(attr_name, locale)
end

def define_getter(attr_name, locale)
define_method localized_attr_name_for(attr_name, locale) do
globalize.stash.contains?(locale, attr_name) ? globalize.send(:fetch_stash, locale, attr_name) : globalize.send(:fetch_attribute, locale, attr_name)
end
end

def define_setter(attr_name, locale)
localized_attr_name = localized_attr_name_for(attr_name, locale)

define_method :"#{localized_attr_name}=" do |value|
attribute_will_change!(localized_attr_name) if value != send(localized_attr_name)
write_attribute(attr_name, value, :locale => locale)
translation_for(locale)[attr_name] = value
end
if respond_to?(:accessible_attributes) && accessible_attributes.include?(attr_name)
attr_accessible :"#{localized_attr_name}"
end
self.globalize_attribute_names << localized_attr_name.to_sym
end

def each_attribute_and_locale(options)
options[:attributes].each do |attr_name|
options[:locales].each do |locale|
yield attr_name, locale
end
end
end

module InstanceMethods
def localized_attr_name_for(attr_name, locale)
self.class.localized_attr_name_for(attr_name, locale)
end
end
end

ActiveRecord::Base.extend Globalize::Accessors
36 changes: 0 additions & 36 deletions releaf-core/lib/releaf/rails_ext/validation_error_codes.rb
Original file line number Diff line number Diff line change
@@ -1,36 +0,0 @@
module ActiveModel
class ErrorMessage < String
attr_accessor :error_code, :data

def initialize(message, error_code = nil, data = nil)
super message
@error_code = error_code
@data = data
end
end

class Errors
def add(attribute, message = nil, options = {})
# build error code from message symbol
error_code = normalize_error_code(attribute, message, options)
message = normalize_message(attribute, message, options)
if exception = options[:strict]
exception = ActiveModel::StrictValidationFailed if exception == true
raise exception, full_message(attribute, message)
end

# use customized String subclass with "error_code" attribute
self[attribute] << ErrorMessage.new(message, error_code, options[:data])
end

def normalize_error_code(_attribute, message, options)
if !options[:error_code].blank?
options[:error_code]
elsif message.class == Symbol
message
else
:invalid
end
end
end
end
9 changes: 4 additions & 5 deletions releaf-core/releaf-core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Gem::Specification.new do |s|

s.files = Dir["app/**/*"] + Dir["lib/**/*"] + ["LICENSE"]

s.add_dependency 'rails', '~> 6.0'
s.add_dependency 'activesupport', '~> 6.0'
s.add_dependency 'activerecord', '~> 6.0'
s.add_dependency 'rails', '~> 6.1'
s.add_dependency 'activesupport', '~> 6.1'
s.add_dependency 'activerecord', '~> 6.1'
s.add_dependency 'i18n', '~> 1.8'
s.add_dependency 'sprockets-rails', '~> 3.0'
s.add_dependency 'sass-rails', '~> 6.0'
Expand All @@ -31,8 +31,7 @@ Gem::Specification.new do |s|
s.add_dependency 'acts_as_list', '~> 0.8'
s.add_dependency 'will_paginate', '~> 3.1'
s.add_dependency 'font-awesome-rails', '~> 4.6'
s.add_dependency 'globalize', '~> 5.3'
s.add_dependency 'globalize-accessors', '~> 0.2.1'
s.add_dependency 'globalize', '~> 6.0'
s.add_dependency 'rack-cache', '~> 1.0'
s.add_dependency 'virtus', '~> 1.0'
end
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,21 @@
end

describe "#column_type" do
let(:column){ Book.columns_hash["id"] }

before do
subject.attribute_name = "birth_date"
allow(subject).to receive(:columns_class).and_return(Author)
allow(column).to receive(:type).and_return("doubleinteger")
end

it "returns column type from columns class" do
allow(Author.columns_hash).to receive(:[]).with("birth_date").and_return(column)
expect(subject.column_type).to eq("doubleinteger")
expect(subject.column_type).to eq(:date)
end

context "when attribute does not exists within columns hash" do
it "returns `string` as default type" do
allow(Author.columns_hash).to receive(:[]).with("birth_date").and_return(nil)
subject.attribute_name = "foo"
expect(subject.column_type).to eq(:string)
end
end

it "caches resolved column type" do
expect(Author.columns_hash).to receive(:[]).with("birth_date").and_return(column).twice
subject.column_type
subject.column_type
subject.column_type
end
end

describe "#columns_class" do
Expand Down
Loading

0 comments on commit dba2e47

Please sign in to comment.