Skip to content

Commit

Permalink
change alias_method_chain with prepend, remove rails5 warnings, ruby …
Browse files Browse the repository at this point in the history
…>= 2.0 required
  • Loading branch information
scambra committed Oct 7, 2016
1 parent 711e6b4 commit 913adb4
Show file tree
Hide file tree
Showing 26 changed files with 206 additions and 232 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Overview
[![Inline docs](https://inch-ci.org/github/activescaffold/active_scaffold.svg?branch=master)](https://inch-ci.org/github/activescaffold/active_scaffold)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)

ActiveScaffold provides a quick and powerful user interfaces for CRUD (create, read, update, delete) operations for Rails applications. It offers additonal features including searching, pagination & layout control. Rails >= 4.0.5 is supported, ruby >= 2.1 supported, although it should work with ruby >= 1.9.3, it's too old and not tested.
ActiveScaffold provides a quick and powerful user interfaces for CRUD (create, read, update, delete) operations for Rails applications. It offers additonal features including searching, pagination & layout control. Rails >= 4.0.5 is supported, ruby >= 2.1 supported, although it should work with ruby >= 2.0.0, it's too old and not tested. Ruby < 2.0.0 won't work.

Branch Details
--------------
Expand Down
2 changes: 1 addition & 1 deletion active_scaffold.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |s|
s.license = 'MIT'
s.test_files = `git ls-files test`.split("\n")

s.required_ruby_version = '>= 1.9'
s.required_ruby_version = '>= 2.0'

s.add_runtime_dependency('rails', '>= 4.0.5')
s.add_runtime_dependency('ice_nine', '~> 0.11')
Expand Down
1 change: 1 addition & 0 deletions bin/nokogiri
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
#
# This file was generated by Bundler.
#
Expand Down
29 changes: 14 additions & 15 deletions lib/active_scaffold/bridges/ancestry/ancestry_bridge.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
ActiveScaffold::Config::Core.class_eval do
def initialize_with_ancestry(model_id)
initialize_without_ancestry(model_id)
module ActiveScaffold::Bridges
class Ancestry
module AncestryBridge
def initialize(model_id)
super(model_id)

return unless model.respond_to? :ancestry_column
return unless model.respond_to? :ancestry_column

columns << :parent_id
columns[:parent_id].form_ui = :ancestry
update.columns.exclude :ancestry
create.columns.exclude :ancestry, :parent_id
list.columns.exclude :ancestry, :parent_id
end

alias_method_chain :initialize, :ancestry
end
columns << :parent_id
columns[:parent_id].form_ui = :ancestry
update.columns.exclude :ancestry
create.columns.exclude :ancestry, :parent_id
list.columns.exclude :ancestry, :parent_id
end
end

module ActiveScaffold::Bridges
class Ancestry
module FormColumnHelpers
def active_scaffold_input_ancestry(column, options)
record = options[:object]
Expand All @@ -39,3 +37,4 @@ def active_scaffold_input_ancestry(column, options)
ActionView::Base.class_eval do
include ActiveScaffold::Bridges::Ancestry::FormColumnHelpers
end
ActiveScaffold::Config::Core.send :prepend, ActiveScaffold::Bridges::Ancestry::AncestryBridge
2 changes: 1 addition & 1 deletion lib/active_scaffold/bridges/bitfields.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ActiveScaffold::Bridges::Bitfields < ActiveScaffold::DataStructures::Bridge
def self.install
require File.join(File.dirname(__FILE__), 'bitfields/bitfields_bridge')
ActiveScaffold::Config::Core.send :include, ActiveScaffold::Bridges::Bitfields::BitfieldsBridge
ActiveScaffold::Config::Core.send :prepend, ActiveScaffold::Bridges::Bitfields::BitfieldsBridge
end
end
13 changes: 4 additions & 9 deletions lib/active_scaffold/bridges/bitfields/bitfields_bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module ActiveScaffold
module Bridges
class Bitfields
module BitfieldsBridge
def initialize_with_bitfields(model_id)
initialize_without_bitfields(model_id)
def initialize(model_id)
super
return unless model.respond_to?(:bitfields) && model.bitfields.present?

model.bitfields.each do |_, options|
Expand All @@ -15,7 +15,7 @@ def initialize_with_bitfields(model_id)
end
end

def _load_action_columns_with_bitfields
def _load_action_columns
model.bitfields.each do |column_name, options|
columns = options.keys.sort_by { |column| self.columns[column].weight }
[:create, :update, :show, :subform].each do |action|
Expand All @@ -29,12 +29,7 @@ def _load_action_columns_with_bitfields
end
end if model.respond_to?(:bitfields) && model.bitfields.present?

_load_action_columns_without_bitfields
end

def self.included(base)
base.alias_method_chain :initialize, :bitfields
base.alias_method_chain :_load_action_columns, :bitfields
super
end
end
end
Expand Down
7 changes: 0 additions & 7 deletions lib/active_scaffold/bridges/calendar_date_select.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
class ActiveScaffold::Bridges::CalendarDateSelect < ActiveScaffold::DataStructures::Bridge
def self.install
# check to see if the old bridge was installed. If so, warn them
# we can detect this by checking to see if the bridge was installed before calling this code

if ActiveScaffold::Config::Core.method_defined?(:initialize_with_calendar_date_select)
raise "We've detected that you have active_scaffold_calendar_date_select_bridge installed. This plugin has been moved to core. Please remove active_scaffold_calendar_date_select_bridge to prevent any conflicts"
end

require File.join(File.dirname(__FILE__), 'calendar_date_select/as_cds_bridge.rb')
end

Expand Down
31 changes: 14 additions & 17 deletions lib/active_scaffold/bridges/calendar_date_select/as_cds_bridge.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
module ActiveScaffold::Config
class Core < Base
def initialize_with_calendar_date_select(model_id)
initialize_without_calendar_date_select(model_id)

calendar_date_select_fields = _columns.collect { |c| c.name.to_sym if [:date, :datetime].include?(c.type) }.compact
# check to see if file column was used on the model
return if calendar_date_select_fields.empty?

# automatically set the forum_ui to a file column
calendar_date_select_fields.each { |field| columns[field].form_ui = :calendar_date_select }
end

alias_method_chain :initialize, :calendar_date_select
end
end

module ActiveScaffold
module Bridges
class CalendarDateSelect
module CalendarDateSelectBridge
def initialize(model_id)
initialize_without_calendar_date_select(model_id)

calendar_date_select_fields = _columns.collect { |c| c.name.to_sym if [:date, :datetime].include?(c.type) }.compact
# check to see if file column was used on the model
return if calendar_date_select_fields.empty?

# automatically set the forum_ui to a file column
calendar_date_select_fields.each { |field| columns[field].form_ui = :calendar_date_select }
end
end

# Helpers that assist with the rendering of a Form Column
module FormColumnHelpers
def active_scaffold_input_calendar_date_select(column, options)
Expand Down Expand Up @@ -61,3 +57,4 @@ def active_scaffold_search_date_bridge_calendar_control(column, options, current
include ActiveScaffold::Bridges::Shared::DateBridge::Finder::ClassMethods
alias_method :condition_for_calendar_date_select_type, :condition_for_date_bridge_type
end
ActiveScaffold::Config::Core.send :prepend, ActiveScaffold::Bridges::CalendarDateSelect::CalendarDateSelectBridge
6 changes: 3 additions & 3 deletions lib/active_scaffold/bridges/cancan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ class ActiveScaffold::Bridges::Cancan < ActiveScaffold::DataStructures::Bridge
def self.install
require File.join(File.dirname(__FILE__), 'cancan', 'cancan_bridge.rb')

ActiveScaffold::Core::ClassMethods.send :include, ActiveScaffold::Bridges::Cancan::ClassMethods
ActiveScaffold::Actions::Core.send :include, ActiveScaffold::Bridges::Cancan::Actions::Core
ActiveScaffold::Actions::Nested.send :include, ActiveScaffold::Bridges::Cancan::Actions::Core
ActiveScaffold::Core::ClassMethods.send :prepend, ActiveScaffold::Bridges::Cancan::ClassMethods
ActiveScaffold::Actions::Core.send :prepend, ActiveScaffold::Bridges::Cancan::Actions::Core
ActiveScaffold::Actions::Nested.send :prepend, ActiveScaffold::Bridges::Cancan::Actions::Core
ActionController::Base.send :include, ActiveScaffold::Bridges::Cancan::ModelUserAccess::Controller
ActionView::Base.send :include, ActiveScaffold::Bridges::Cancan::AssociationHelpers
::ActiveRecord::Base.send :include, ActiveScaffold::Bridges::Cancan::ModelUserAccess::Model
Expand Down
28 changes: 8 additions & 20 deletions lib/active_scaffold/bridges/cancan/cancan_bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@ class Cancan
# As already has callbacks to ensure authorization at controller method via "authorization_method"
# but let's include this too, just in case, no sure how performance is affected tough :TODO benchmark
module ClassMethods
extend ActiveSupport::Concern
included do
alias_method_chain :active_scaffold, :cancan
end

def active_scaffold_with_cancan(model_id = nil, &block)
active_scaffold_without_cancan(model_id, &block)
def active_scaffold(model_id = nil, &block)
super
authorize_resource(
:class => active_scaffold_config.model,
:instance => :record
Expand All @@ -46,13 +41,9 @@ def association_klass_scoped(association, klass, record)
# beginning of chain integration
module Actions
module Core
extend ActiveSupport::Concern
included do
alias_method_chain :beginning_of_chain, :cancan
end
# :TODO can this be expanded more ?
def beginning_of_chain_with_cancan
beginning_of_chain_without_cancan.accessible_by(current_ability)
def beginning_of_chain
super.accessible_by(current_ability)
end
end
end
Expand Down Expand Up @@ -103,11 +94,9 @@ def current_ability
module ActiveRecord
extend ActiveSupport::Concern
included do
extend SecurityMethods
include SecurityMethods
alias_method_chain :authorized_for?, :cancan
prepend SecurityMethods
class << self
alias_method_chain :authorized_for?, :cancan
prepend SecurityMethods
end
end

Expand All @@ -119,16 +108,15 @@ class InvalidArgument < StandardError; end
# {:action=>"edit"}
# to allow access cancan must allow both :crud_type and :action
# if cancan says "no", it delegates to default AS behavior
def authorized_for_with_cancan?(options = {})
def authorized_for?(options = {})
raise InvalidArgument if options[:crud_type].blank? && options[:action].blank?
if current_ability.present?
crud_type_result = options[:crud_type].nil? ? true : current_ability.can?(options[:crud_type], self)
action_result = options[:action].nil? ? true : current_ability.can?(options[:action].to_sym, self)
else
crud_type_result, action_result = false, false
end
default_result = authorized_for_without_cancan?(options)
(crud_type_result && action_result) || default_result
(crud_type_result && action_result) || super
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_scaffold/bridges/carrierwave.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def self.install
require File.join(File.dirname(__FILE__), 'carrierwave/list_ui')
require File.join(File.dirname(__FILE__), 'carrierwave/carrierwave_bridge_helpers')
require File.join(File.dirname(__FILE__), 'carrierwave/carrierwave_bridge')
ActiveScaffold::Config::Core.send :include, ActiveScaffold::Bridges::Carrierwave::CarrierwaveBridge
ActiveScaffold::Config::Core.send :prepend, ActiveScaffold::Bridges::Carrierwave::CarrierwaveBridge
end

def self.install?
Expand Down
8 changes: 2 additions & 6 deletions lib/active_scaffold/bridges/carrierwave/carrierwave_bridge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module ActiveScaffold
module Bridges
class Carrierwave
module CarrierwaveBridge
def initialize_with_carrierwave(model_id)
initialize_without_carrierwave(model_id)
def initialize(model_id)
super
return unless model.respond_to?(:uploaders) && model.uploaders.present?

update.multipart = true
Expand All @@ -14,10 +14,6 @@ def initialize_with_carrierwave(model_id)
end
end

def self.included(base)
base.alias_method_chain :initialize, :carrierwave
end

private

def configure_carrierwave_field(field)
Expand Down
78 changes: 41 additions & 37 deletions lib/active_scaffold/bridges/date_picker/ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,58 @@ def self.binread(file)
end
end

ActiveScaffold::Config::Core.class_eval do
def initialize_with_date_picker(model_id)
initialize_without_date_picker(model_id)
class ActiveScaffold::Bridges::DatePicker
module DatePickerBridge
def initialize(model_id)
super

date_picker_fields = _columns.collect { |c| {:name => c.name.to_sym, :type => c.type} if [:date, :datetime].include?(c.type) }.compact
# check to see if file column was used on the model
return if date_picker_fields.empty?
date_picker_fields = _columns.collect { |c| {:name => c.name.to_sym, :type => c.type} if [:date, :datetime].include?(c.type) }.compact
# check to see if file column was used on the model
return if date_picker_fields.empty?

# automatically set the forum_ui to a date_picker or datetime_picker
date_picker_fields.each do |field|
col_config = columns[field[:name]]
col_config.form_ui = (field[:type] == :date ? :date_picker : :datetime_picker)
# automatically set the forum_ui to a date_picker or datetime_picker
date_picker_fields.each do |field|
col_config = columns[field[:name]]
col_config.form_ui = (field[:type] == :date ? :date_picker : :datetime_picker)
end
end
end

alias_method_chain :initialize, :date_picker
end
module Finder
def datetime_conversion_for_condition(column)
if column.search_ui == :date_picker
:to_date
else
super
end
end
end

module ActiveScaffold::Bridges::DatePicker::CastExtension
def fallback_string_to_date_with_date_picker(string)
Date.strptime(string, I18n.t('date.formats.default')) rescue fallback_string_to_date_without_date_picker(string)
module AttributeParams
def datetime_conversion_for_value(column)
if column.form_ui == :date_picker
:to_date
else
super
end
end
end

def self.included(base)
base.alias_method_chain :fallback_string_to_date, :date_picker
module CastExtension
def fallback_string_to_date(string)
Date.strptime(string, I18n.t('date.formats.default')) rescue super
end
end
end
if defined?(ActiveRecord::ConnectionAdapters::Type)
ActiveRecord::ConnectionAdapters::Type::Date.send(:include, ActiveScaffold::Bridges::DatePicker::CastExtension)
if defined?(ActiveRecord::ConnectionAdapters::Type) # rails >= 4.2
ActiveRecord::ConnectionAdapters::Type::Date.send(:prepend, ActiveScaffold::Bridges::DatePicker::CastExtension)
else
ActiveRecord::ConnectionAdapters::Column.extend ActiveScaffold::Bridges::DatePicker::CastExtension
class << ActiveRecord::ConnectionAdapters::Column
prepend ActiveScaffold::Bridges::DatePicker::CastExtension
end
end

ActiveScaffold::Config::Core.send :prepend, ActiveScaffold::Bridges::DatePicker::DatePickerBridge
ActionView::Base.class_eval do
include ActiveScaffold::Bridges::Shared::DateBridge::SearchColumnHelpers
alias_method :active_scaffold_search_date_picker, :active_scaffold_search_date_bridge
Expand All @@ -52,28 +71,13 @@ def self.included(base)
include ActiveScaffold::Bridges::DatePicker::Helper::DatepickerColumnHelpers
end
ActiveScaffold::Finder::ClassMethods.module_eval do
prepend ActiveScaffold::Bridges::DatePicker::Finder
include ActiveScaffold::Bridges::Shared::DateBridge::Finder::ClassMethods
def datetime_conversion_for_condition_with_datepicker(column)
if column.search_ui == :date_picker
:to_date
else
datetime_conversion_for_condition_without_datepicker(column)
end
end
alias_method_chain :datetime_conversion_for_condition, :datepicker

alias_method :condition_for_date_picker_type, :condition_for_date_bridge_type
alias_method :condition_for_datetime_picker_type, :condition_for_date_picker_type
end
ActiveScaffold::AttributeParams.module_eval do
def datetime_conversion_for_value_with_datepicker(column)
if column.form_ui == :date_picker
:to_date
else
datetime_conversion_for_value_without_datepicker(column)
end
end
alias_method_chain :datetime_conversion_for_value, :datepicker
prepend ActiveScaffold::Bridges::DatePicker::AttributeParams
alias_method :column_value_for_date_picker_type, :column_value_for_datetime_type
alias_method :column_value_for_datetime_picker_type, :column_value_for_datetime_type
end
2 changes: 1 addition & 1 deletion lib/active_scaffold/bridges/dragonfly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ def self.install
require File.join(File.dirname(__FILE__), 'dragonfly/list_ui')
require File.join(File.dirname(__FILE__), 'dragonfly/dragonfly_bridge_helpers')
require File.join(File.dirname(__FILE__), 'dragonfly/dragonfly_bridge')
ActiveScaffold::Config::Core.send :include, ActiveScaffold::Bridges::Dragonfly::DragonflyBridge
ActiveScaffold::Config::Core.send :prepend, ActiveScaffold::Bridges::Dragonfly::DragonflyBridge
end
end
Loading

0 comments on commit 913adb4

Please sign in to comment.