From 913adb496223fcc645a7f1fe44c43ed7128c43c0 Mon Sep 17 00:00:00 2001 From: Sergio Cambra Date: Fri, 7 Oct 2016 14:51:08 +0200 Subject: [PATCH] change alias_method_chain with prepend, remove rails5 warnings, ruby >= 2.0 required --- README.md | 2 +- active_scaffold.gemspec | 2 +- bin/nokogiri | 1 + .../bridges/ancestry/ancestry_bridge.rb | 29 ++++--- lib/active_scaffold/bridges/bitfields.rb | 2 +- .../bridges/bitfields/bitfields_bridge.rb | 13 +--- .../bridges/calendar_date_select.rb | 7 -- .../calendar_date_select/as_cds_bridge.rb | 31 ++++---- lib/active_scaffold/bridges/cancan.rb | 6 +- .../bridges/cancan/cancan_bridge.rb | 28 ++----- lib/active_scaffold/bridges/carrierwave.rb | 2 +- .../bridges/carrierwave/carrierwave_bridge.rb | 8 +- .../bridges/date_picker/ext.rb | 78 ++++++++++--------- lib/active_scaffold/bridges/dragonfly.rb | 2 +- .../bridges/dragonfly/dragonfly_bridge.rb | 8 +- .../file_column/as_file_column_bridge.rb | 11 ++- .../bridges/paper_trail/paper_trail_bridge.rb | 10 +-- lib/active_scaffold/bridges/paperclip.rb | 2 +- .../bridges/paperclip/paperclip_bridge.rb | 8 +- .../bridges/semantic_attributes/column.rb | 10 +-- .../bridges/tiny_mce/helpers.rb | 24 +++--- .../extensions/action_controller_rendering.rb | 15 ++-- .../extensions/action_view_rendering.rb | 30 ++++--- .../extensions/name_option_for_datetime.rb | 16 ++-- .../extensions/paginator_extensions.rb | 52 +++++++------ lib/active_scaffold/tableless.rb | 41 +++++----- 26 files changed, 206 insertions(+), 232 deletions(-) diff --git a/README.md b/README.md index fe8f743dc7..743e8674f8 100644 --- a/README.md +++ b/README.md @@ -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 -------------- diff --git a/active_scaffold.gemspec b/active_scaffold.gemspec index d8ff839340..db562b7476 100644 --- a/active_scaffold.gemspec +++ b/active_scaffold.gemspec @@ -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') diff --git a/bin/nokogiri b/bin/nokogiri index d5ae0825ed..c1f0ca44fe 100755 --- a/bin/nokogiri +++ b/bin/nokogiri @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# frozen_string_literal: true # # This file was generated by Bundler. # diff --git a/lib/active_scaffold/bridges/ancestry/ancestry_bridge.rb b/lib/active_scaffold/bridges/ancestry/ancestry_bridge.rb index b2c2f1681f..fdb5ea28a3 100644 --- a/lib/active_scaffold/bridges/ancestry/ancestry_bridge.rb +++ b/lib/active_scaffold/bridges/ancestry/ancestry_bridge.rb @@ -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] @@ -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 diff --git a/lib/active_scaffold/bridges/bitfields.rb b/lib/active_scaffold/bridges/bitfields.rb index 1f0c2fdb02..6e947157f5 100644 --- a/lib/active_scaffold/bridges/bitfields.rb +++ b/lib/active_scaffold/bridges/bitfields.rb @@ -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 diff --git a/lib/active_scaffold/bridges/bitfields/bitfields_bridge.rb b/lib/active_scaffold/bridges/bitfields/bitfields_bridge.rb index 508eff7289..4e0d808dcd 100644 --- a/lib/active_scaffold/bridges/bitfields/bitfields_bridge.rb +++ b/lib/active_scaffold/bridges/bitfields/bitfields_bridge.rb @@ -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| @@ -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| @@ -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 diff --git a/lib/active_scaffold/bridges/calendar_date_select.rb b/lib/active_scaffold/bridges/calendar_date_select.rb index fd0385ca16..eb98563b2a 100644 --- a/lib/active_scaffold/bridges/calendar_date_select.rb +++ b/lib/active_scaffold/bridges/calendar_date_select.rb @@ -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 diff --git a/lib/active_scaffold/bridges/calendar_date_select/as_cds_bridge.rb b/lib/active_scaffold/bridges/calendar_date_select/as_cds_bridge.rb index fb06211663..ccfd821987 100644 --- a/lib/active_scaffold/bridges/calendar_date_select/as_cds_bridge.rb +++ b/lib/active_scaffold/bridges/calendar_date_select/as_cds_bridge.rb @@ -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) @@ -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 diff --git a/lib/active_scaffold/bridges/cancan.rb b/lib/active_scaffold/bridges/cancan.rb index b0348bd798..43b2f643c3 100644 --- a/lib/active_scaffold/bridges/cancan.rb +++ b/lib/active_scaffold/bridges/cancan.rb @@ -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 diff --git a/lib/active_scaffold/bridges/cancan/cancan_bridge.rb b/lib/active_scaffold/bridges/cancan/cancan_bridge.rb index 9b81d5c235..9121ac6b5e 100644 --- a/lib/active_scaffold/bridges/cancan/cancan_bridge.rb +++ b/lib/active_scaffold/bridges/cancan/cancan_bridge.rb @@ -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 @@ -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 @@ -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 @@ -119,7 +108,7 @@ 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) @@ -127,8 +116,7 @@ def authorized_for_with_cancan?(options = {}) 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 diff --git a/lib/active_scaffold/bridges/carrierwave.rb b/lib/active_scaffold/bridges/carrierwave.rb index 6d34b1ba0a..4181503c85 100644 --- a/lib/active_scaffold/bridges/carrierwave.rb +++ b/lib/active_scaffold/bridges/carrierwave.rb @@ -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? diff --git a/lib/active_scaffold/bridges/carrierwave/carrierwave_bridge.rb b/lib/active_scaffold/bridges/carrierwave/carrierwave_bridge.rb index cb7f89d249..a86145fc6f 100644 --- a/lib/active_scaffold/bridges/carrierwave/carrierwave_bridge.rb +++ b/lib/active_scaffold/bridges/carrierwave/carrierwave_bridge.rb @@ -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 @@ -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) diff --git a/lib/active_scaffold/bridges/date_picker/ext.rb b/lib/active_scaffold/bridges/date_picker/ext.rb index d06b01458e..4bf9e9b6c2 100644 --- a/lib/active_scaffold/bridges/date_picker/ext.rb +++ b/lib/active_scaffold/bridges/date_picker/ext.rb @@ -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 @@ -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 diff --git a/lib/active_scaffold/bridges/dragonfly.rb b/lib/active_scaffold/bridges/dragonfly.rb index ee75a8b0a3..d986a7036b 100644 --- a/lib/active_scaffold/bridges/dragonfly.rb +++ b/lib/active_scaffold/bridges/dragonfly.rb @@ -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 diff --git a/lib/active_scaffold/bridges/dragonfly/dragonfly_bridge.rb b/lib/active_scaffold/bridges/dragonfly/dragonfly_bridge.rb index aa32c8fcb8..b939b90e9e 100644 --- a/lib/active_scaffold/bridges/dragonfly/dragonfly_bridge.rb +++ b/lib/active_scaffold/bridges/dragonfly/dragonfly_bridge.rb @@ -2,8 +2,8 @@ module ActiveScaffold module Bridges class Dragonfly module DragonflyBridge - def initialize_with_dragonfly(model_id) - initialize_without_dragonfly(model_id) + def initialize(model_id) + super return unless model.respond_to?(:dragonfly_attachment_classes) && model.dragonfly_attachment_classes.present? update.multipart = true @@ -14,10 +14,6 @@ def initialize_with_dragonfly(model_id) end end - def self.included(base) - base.alias_method_chain :initialize, :dragonfly - end - private def configure_dragonfly_field(field) diff --git a/lib/active_scaffold/bridges/file_column/as_file_column_bridge.rb b/lib/active_scaffold/bridges/file_column/as_file_column_bridge.rb index 0feb104a5c..5e9aa687f0 100644 --- a/lib/active_scaffold/bridges/file_column/as_file_column_bridge.rb +++ b/lib/active_scaffold/bridges/file_column/as_file_column_bridge.rb @@ -2,11 +2,11 @@ attr_accessor :file_column_display end -module ActiveScaffold::Config - class Core < Base +class ActiveScaffold::Bridges::FileColumn + module FileColumnBridge attr_accessor :file_column_fields - def initialize_with_file_column(model_id) - initialize_without_file_column(model_id) + def initialize(model_id) + super return unless ActiveScaffold::Bridges::FileColumn::FileColumnHelpers.klass_has_file_column_fields?(model) @@ -22,8 +22,6 @@ def initialize_with_file_column(model_id) model.file_column_fields.each { |field| configure_file_column_field(field) } end - alias_method_chain :initialize, :file_column unless method_defined?(:initialize_without_file_column) - def configure_file_column_field(field) # set list_ui first because it gets its default value from form_ui columns[field].list_ui ||= model.field_has_image_version?(field, 'thumb') ? :thumbnail : :download_link_with_filename @@ -41,3 +39,4 @@ def configure_file_column_field(field) end end end +ActiveScaffold::Config::Core.send :prepend, ActiveScaffold::Bridges::FileColumn::FileColumnBridge diff --git a/lib/active_scaffold/bridges/paper_trail/paper_trail_bridge.rb b/lib/active_scaffold/bridges/paper_trail/paper_trail_bridge.rb index a2cefb1b22..68c0364ae9 100644 --- a/lib/active_scaffold/bridges/paper_trail/paper_trail_bridge.rb +++ b/lib/active_scaffold/bridges/paper_trail/paper_trail_bridge.rb @@ -2,17 +2,13 @@ module ActiveScaffold module Bridges class PaperTrail module PaperTrailBridge - def initialize_with_paper_trail(model_id) - initialize_without_paper_trail(model_id) + def initialize(model_id) + super return unless model < ::PaperTrail::Model::InstanceMethods actions << :deleted_records end - - def self.included(base) - base.alias_method_chain :initialize, :paper_trail - end end end end end -ActiveScaffold::Config::Core.send :include, ActiveScaffold::Bridges::PaperTrail::PaperTrailBridge +ActiveScaffold::Config::Core.send :prepend, ActiveScaffold::Bridges::PaperTrail::PaperTrailBridge diff --git a/lib/active_scaffold/bridges/paperclip.rb b/lib/active_scaffold/bridges/paperclip.rb index 65d7547c1a..22caf883a4 100644 --- a/lib/active_scaffold/bridges/paperclip.rb +++ b/lib/active_scaffold/bridges/paperclip.rb @@ -7,6 +7,6 @@ def self.install require File.join(File.dirname(__FILE__), 'paperclip/list_ui') require File.join(File.dirname(__FILE__), 'paperclip/paperclip_bridge_helpers') require File.join(File.dirname(__FILE__), 'paperclip/paperclip_bridge') - ActiveScaffold::Config::Core.send :include, ActiveScaffold::Bridges::Paperclip::PaperclipBridge + ActiveScaffold::Config::Core.send :prepend, ActiveScaffold::Bridges::Paperclip::PaperclipBridge end end diff --git a/lib/active_scaffold/bridges/paperclip/paperclip_bridge.rb b/lib/active_scaffold/bridges/paperclip/paperclip_bridge.rb index 2e5b3b1783..9a7f254539 100644 --- a/lib/active_scaffold/bridges/paperclip/paperclip_bridge.rb +++ b/lib/active_scaffold/bridges/paperclip/paperclip_bridge.rb @@ -2,8 +2,8 @@ module ActiveScaffold module Bridges class Paperclip module PaperclipBridge - def initialize_with_paperclip(model_id) - initialize_without_paperclip(model_id) + def initialize(model_id) + super return unless model.respond_to?(:attachment_definitions) && !model.attachment_definitions.nil? update.multipart = true @@ -16,10 +16,6 @@ def initialize_with_paperclip(model_id) end end - def self.included(base) - base.alias_method_chain :initialize, :paperclip - end - private def configure_paperclip_field(field) diff --git a/lib/active_scaffold/bridges/semantic_attributes/column.rb b/lib/active_scaffold/bridges/semantic_attributes/column.rb index 68de66e04a..146706db4b 100644 --- a/lib/active_scaffold/bridges/semantic_attributes/column.rb +++ b/lib/active_scaffold/bridges/semantic_attributes/column.rb @@ -1,11 +1,7 @@ class ActiveScaffold::Bridges::SemanticAttributes module Column - def self.included(base) - base.class_eval { alias_method_chain :initialize, :semantic_attributes } - end - - def initialize_with_semantic_attributes(name, active_record_class) - initialize_without_semantic_attributes(name, active_record_class) + def initialize(name, active_record_class) + super self.required = !active_record_class.semantic_attributes[self.name].predicates.find { |p| p.allow_empty? == false }.nil? active_record_class.semantic_attributes[self.name].predicates.find do |p| sem_type = p.class.to_s.split('::')[1].underscore.to_sym @@ -16,5 +12,5 @@ def initialize_with_semantic_attributes(name, active_record_class) end end ActiveScaffold::DataStructures::Column.class_eval do - include ActiveScaffold::Bridges::SemanticAttributes::Column + prepend ActiveScaffold::Bridges::SemanticAttributes::Column end diff --git a/lib/active_scaffold/bridges/tiny_mce/helpers.rb b/lib/active_scaffold/bridges/tiny_mce/helpers.rb index 058607b767..5ce073e7cb 100644 --- a/lib/active_scaffold/bridges/tiny_mce/helpers.rb +++ b/lib/active_scaffold/bridges/tiny_mce/helpers.rb @@ -9,7 +9,19 @@ def self.included(base) module FormColumnHelpers def self.included(base) - base.alias_method_chain :onsubmit, :tiny_mce + base.prepend OnSubmit + end + + module OnSubmit + def onsubmit + case ActiveScaffold.js_framework + when :jquery + submit_js = 'tinyMCE.triggerSave();jQuery(\'textarea.mceEditor\').each(function(index, elem) { tinyMCE.execCommand(\'mceRemoveEditor\', false, jQuery(elem).attr(\'id\')); });' + when :prototype + submit_js = 'tinyMCE.triggerSave();this.select(\'textarea.mceEditor\').each(function(elem) { tinyMCE.execCommand(\'mceRemoveEditor\', false, elem.id); });' + end + [super, submit_js].compact.join ';' + end end # The two column options that can be set specifically for the text_editor input @@ -33,16 +45,6 @@ def active_scaffold_input_text_editor(column, options) html.join "\n" end - def onsubmit_with_tiny_mce - case ActiveScaffold.js_framework - when :jquery - submit_js = 'tinyMCE.triggerSave();jQuery(\'textarea.mceEditor\').each(function(index, elem) { tinyMCE.execCommand(\'mceRemoveEditor\', false, jQuery(elem).attr(\'id\')); });' - when :prototype - submit_js = 'tinyMCE.triggerSave();this.select(\'textarea.mceEditor\').each(function(elem) { tinyMCE.execCommand(\'mceRemoveEditor\', false, elem.id); });' - end - [onsubmit_without_tiny_mce, submit_js].compact.join ';' - end - # The implementation is very tinymce specific, so it makes sense allowing :form_ui # to be :tinymce as well alias active_scaffold_input_tinymce active_scaffold_input_text_editor diff --git a/lib/active_scaffold/extensions/action_controller_rendering.rb b/lib/active_scaffold/extensions/action_controller_rendering.rb index c8c183ea2b..abc8957b78 100644 --- a/lib/active_scaffold/extensions/action_controller_rendering.rb +++ b/lib/active_scaffold/extensions/action_controller_rendering.rb @@ -1,7 +1,7 @@ # wrap the action rendering for ActiveScaffold controllers -module ActionController #:nodoc: - class Base - def render_with_active_scaffold(*args, &block) +module ActiveScaffold + module ActionController #:nodoc: + def render(*args, &block) if self.class.uses_active_scaffold? && params[:adapter] && @rendering_adapter.nil? && request.xhr? @rendering_adapter = true # recursion control # if we need an adapter, then we render the actual stuff to a string and insert it into the adapter template @@ -11,9 +11,14 @@ def render_with_active_scaffold(*args, &block) :use_full_path => true, :layout => false, :content_type => :html @rendering_adapter = nil # recursion control else - render_without_active_scaffold(*args, &block) + super(*args, &block) end end - alias_method_chain :render, :active_scaffold + end +end + +module ActionController + class Base + prepend ActiveScaffold::ActionController end end diff --git a/lib/active_scaffold/extensions/action_view_rendering.rb b/lib/active_scaffold/extensions/action_view_rendering.rb index ffeb5a334f..0707f20bf0 100644 --- a/lib/active_scaffold/extensions/action_view_rendering.rb +++ b/lib/active_scaffold/extensions/action_view_rendering.rb @@ -1,16 +1,15 @@ -module ActionView - class LookupContext +module ActiveScaffold + module LookupContext attr_accessor :last_template - def find_template_with_last_template(name, prefixes = [], partial = false, keys = [], options = {}) - self.last_template = find_template_without_last_template(name, prefixes, partial, keys, options) + def find_template(name, prefixes = [], partial = false, keys = [], options = {}) + self.last_template = super(name, prefixes, partial, keys, options) end - alias_method_chain :find_template, :last_template end end # wrap the action rendering for ActiveScaffold views -module ActionView::Helpers #:nodoc: +module ActiveScaffold #:nodoc: module RenderingHelper # # Adds two rendering options. @@ -34,7 +33,7 @@ module RenderingHelper # # Defining options[:label] lets you completely customize the list title for the embedded scaffold. # - def render_with_active_scaffold(*args, &block) + def render(*args, &block) if args.first.is_a?(Hash) && args.first[:active_scaffold] require 'digest/sha2' options = args.first @@ -102,7 +101,7 @@ def render_with_active_scaffold(*args, &block) last_view_path = File.expand_path(File.dirname(File.dirname(lookup_context.last_template.inspect)), Rails.root) lookup_context.view_paths = view_paths.drop(view_paths.find_index { |path| path.to_s == last_view_path } + 1) end - result = render_without_active_scaffold options + result = super options lookup_context.view_paths = @_view_paths if @_view_paths lookup_context.last_template = @_last_template if @_last_template result @@ -116,16 +115,27 @@ def render_with_active_scaffold(*args, &block) end view_stack << current_view if current_view lookup_context.view_paths = @_view_paths # reset view_paths in case a view render :super, and then render :partial - result = render_without_active_scaffold(*args, &block) + result = super view_stack.pop if current_view.present? lookup_context.last_template = last_template result end end - alias_method_chain :render, :active_scaffold def view_stack @_view_stack ||= [] end end end + +module ActionView + LookupContext.class_eval do + prepend ActiveScaffold::LookupContext + end + + module Helpers + Base.class_eval do + include ActiveScaffold::RenderingHelper + end + end +end diff --git a/lib/active_scaffold/extensions/name_option_for_datetime.rb b/lib/active_scaffold/extensions/name_option_for_datetime.rb index 38c1bc741d..1a2ec348f3 100644 --- a/lib/active_scaffold/extensions/name_option_for_datetime.rb +++ b/lib/active_scaffold/extensions/name_option_for_datetime.rb @@ -1,18 +1,12 @@ module ActiveScaffold module DateSelectExtension - def datetime_selector_with_name(options, html_options) + private + def datetime_selector(options, html_options) options[:prefix] = options[:name].gsub(/\[[^\[]*\]$/, '') if options[:name] - datetime_selector_without_name(options, html_options) - end - - def self.included(base) - base.class_eval do - alias_method_chain :datetime_selector, :name - private :datetime_selector_without_name, :datetime_selector_with_name, :datetime_selector - end + super(options, html_options) end end end -(defined?(ActionView::Helpers::Tags::DateSelect) ? ActionView::Helpers::Tags::DateSelect : ActionView::Helpers::InstanceTag).class_eval do - include ActiveScaffold::DateSelectExtension +ActionView::Helpers::Tags::DateSelect.class_eval do + prepend ActiveScaffold::DateSelectExtension end diff --git a/lib/active_scaffold/extensions/paginator_extensions.rb b/lib/active_scaffold/extensions/paginator_extensions.rb index 966c81b0bd..9ecc2a6fe6 100644 --- a/lib/active_scaffold/extensions/paginator_extensions.rb +++ b/lib/active_scaffold/extensions/paginator_extensions.rb @@ -1,35 +1,37 @@ require 'active_scaffold/paginator' -Paginator.class_eval do - # Total number of pages - def number_of_pages_with_infinite - number_of_pages_without_infinite if @count - end - alias_method_chain :number_of_pages, :infinite +module ActiveScaffold + module Paginator + # Total number of pages + def number_of_pages + super if @count + end - # Is this an "infinite" paginator - def infinite? - @count.nil? - end + # Is this an "infinite" paginator + def infinite? + @count.nil? + end - def count - @count || first.items.size + def count + @count || first.items.size + end end -end -Paginator::Page.class_eval do - # Checks to see if there's a page after this one - def next_with_infinite? - return true if @pager.infinite? - next_without_infinite? - end - alias_method_chain :next?, :infinite + module Page + # Checks to see if there's a page after this one + def next? + @pager.infinite? || super + end - def empty? - if @pager.infinite? - items.to_a.empty? - else - @pager.count.zero? + def empty? + if @pager.infinite? + items.to_a.empty? + else + @pager.count.zero? + end end end end + +Paginator.class_eval { prepend ActiveScaffold::Paginator } +Paginator::Page.class_eval { prepend ActiveScaffold::Page } diff --git a/lib/active_scaffold/tableless.rb b/lib/active_scaffold/tableless.rb index 12d32363ef..b8013a4306 100644 --- a/lib/active_scaffold/tableless.rb +++ b/lib/active_scaffold/tableless.rb @@ -54,15 +54,10 @@ def initialize(name, default, sql_type = nil, null = true) end end - module Association - def self.included(base) - base.alias_method_chain :association_scope, :tableless - base.alias_method_chain :target_scope, :tableless - end - - def association_scope_with_tableless + module Tableless + def association_scope @association_scope ||= overrided_association_scope if klass < ActiveScaffold::Tableless - association_scope_without_tableless + super end def overrided_association_scope @@ -73,32 +68,42 @@ def overrided_association_scope end end - def target_scope_with_tableless - target_scope_without_tableless.tap do |scope| + def target_scope + super.tap do |scope| if klass < ActiveScaffold::Tableless class << scope; include RelationExtension; end end end end end + + module Association + def self.included(base) + base.prepend Tableless + end + end + + module TablelessCollectionAssociation + def get_records + klass < ActiveScaffold::Tableless ? scope.to_a : super + end + end module CollectionAssociation def self.included(base) - base.alias_method_chain :get_records, :tableless if Rails.version >= '4.2' + base.prepend TablelessCollectionAssociation if Rails.version >= '4.2' end + end - def get_records_with_tableless - klass < ActiveScaffold::Tableless ? scope.to_a : get_records_without_tableless + module TablelessSingularAssociation + def get_records + klass < ActiveScaffold::Tableless ? scope.limit(1).to_a : super end end module SingularAssociation def self.included(base) - base.alias_method_chain :get_records, :tableless if Rails.version >= '4.2' - end - - def get_records_with_tableless - klass < ActiveScaffold::Tableless ? scope.limit(1).to_a : get_records_without_tableless + base.prepend TablelessSingularAssociation end end