Skip to content

Commit 67e846f

Browse files
authored
Merge pull request #267 from mattbrictson/fix-alias-deprecation
Fix alias_method_chain deprecation messages in Rails 5
2 parents 941edb9 + 8852f8a commit 67e846f

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Bugfixes:
44

55
- Minor README corrections (#184, @msmithstubbs)
6+
- Fix `alias_method_chain` deprecation warnings when using Rails 5
67

78
Features:
89

lib/bootstrap_form/aliasing.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module BootstrapForm
2+
# This module implements the old ActiveSupport alias_method_chain feature
3+
# with a new name, and without the deprecation warnings. In ActiveSupport 5+,
4+
# this style of patching was deprecated in favor of Module.prepend. But
5+
# Module.prepend is not present in Ruby 1.9, which we would still like to
6+
# support. So we continue to use of alias_method_chain, albeit with a
7+
# different name to avoid collisions.
8+
module Aliasing
9+
# This code is copied and pasted from ActiveSupport, but with :bootstrap
10+
# hardcoded as the feature name, and with the deprecation warning removed.
11+
def bootstrap_method_alias(target)
12+
feature = :bootstrap
13+
14+
# Strip out punctuation on predicates, bang or writer methods since
15+
# e.g. target?_without_feature is not a valid method name.
16+
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
17+
yield(aliased_target, punctuation) if block_given?
18+
19+
with_method = "#{aliased_target}_with_#{feature}#{punctuation}"
20+
without_method = "#{aliased_target}_without_#{feature}#{punctuation}"
21+
22+
alias_method without_method, target
23+
alias_method target, with_method
24+
25+
case
26+
when public_method_defined?(without_method)
27+
public target
28+
when protected_method_defined?(without_method)
29+
protected target
30+
when private_method_defined?(without_method)
31+
private target
32+
end
33+
end
34+
end
35+
end

lib/bootstrap_form/form_builder.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
require_relative 'aliasing'
12
require_relative 'helpers/bootstrap'
23

34
module BootstrapForm
45
class FormBuilder < ActionView::Helpers::FormBuilder
6+
extend BootstrapForm::Aliasing
57
include BootstrapForm::Helpers::Bootstrap
68

79
attr_reader :layout, :label_col, :control_col, :has_error, :inline_errors, :label_errors, :acts_like_form_tag
@@ -42,7 +44,7 @@ def initialize(object_name, object, template, options)
4244
end
4345
end
4446

45-
alias_method_chain method_name, :bootstrap
47+
bootstrap_method_alias method_name
4648
end
4749

4850
DATE_SELECT_HELPERS.each do |method_name|
@@ -55,7 +57,7 @@ def initialize(object_name, object, template, options)
5557
end
5658
end
5759

58-
alias_method_chain method_name, :bootstrap
60+
bootstrap_method_alias method_name
5961
end
6062

6163
def file_field_with_bootstrap(name, options = {})
@@ -64,7 +66,7 @@ def file_field_with_bootstrap(name, options = {})
6466
end
6567
end
6668

67-
alias_method_chain :file_field, :bootstrap
69+
bootstrap_method_alias :file_field
6870

6971
if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new("4.1.0")
7072
def select_with_bootstrap(method, choices = nil, options = {}, html_options = {}, &block)
@@ -80,31 +82,31 @@ def select_with_bootstrap(method, choices, options = {}, html_options = {})
8082
end
8183
end
8284

83-
alias_method_chain :select, :bootstrap
85+
bootstrap_method_alias :select
8486

8587
def collection_select_with_bootstrap(method, collection, value_method, text_method, options = {}, html_options = {})
8688
form_group_builder(method, options, html_options) do
8789
collection_select_without_bootstrap(method, collection, value_method, text_method, options, html_options)
8890
end
8991
end
9092

91-
alias_method_chain :collection_select, :bootstrap
93+
bootstrap_method_alias :collection_select
9294

9395
def grouped_collection_select_with_bootstrap(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
9496
form_group_builder(method, options, html_options) do
9597
grouped_collection_select_without_bootstrap(method, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
9698
end
9799
end
98100

99-
alias_method_chain :grouped_collection_select, :bootstrap
101+
bootstrap_method_alias :grouped_collection_select
100102

101103
def time_zone_select_with_bootstrap(method, priority_zones = nil, options = {}, html_options = {})
102104
form_group_builder(method, options, html_options) do
103105
time_zone_select_without_bootstrap(method, priority_zones, options, html_options)
104106
end
105107
end
106108

107-
alias_method_chain :time_zone_select, :bootstrap
109+
bootstrap_method_alias :time_zone_select
108110

109111
def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_value = "0", &block)
110112
options = options.symbolize_keys!
@@ -130,7 +132,7 @@ def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_
130132
end
131133
end
132134

133-
alias_method_chain :check_box, :bootstrap
135+
bootstrap_method_alias :check_box
134136

135137
def radio_button_with_bootstrap(name, value, *args)
136138
options = args.extract_options!.symbolize_keys!
@@ -151,7 +153,7 @@ def radio_button_with_bootstrap(name, value, *args)
151153
end
152154
end
153155

154-
alias_method_chain :radio_button, :bootstrap
156+
bootstrap_method_alias :radio_button
155157

156158
def collection_check_boxes_with_bootstrap(*args)
157159
html = inputs_collection(*args) do |name, value, options|
@@ -161,15 +163,15 @@ def collection_check_boxes_with_bootstrap(*args)
161163
hidden_field(args.first,{value: "", multiple: true}).concat(html)
162164
end
163165

164-
alias_method_chain :collection_check_boxes, :bootstrap
166+
bootstrap_method_alias :collection_check_boxes
165167

166168
def collection_radio_buttons_with_bootstrap(*args)
167169
inputs_collection(*args) do |name, value, options|
168170
radio_button(name, value, options)
169171
end
170172
end
171173

172-
alias_method_chain :collection_radio_buttons, :bootstrap
174+
bootstrap_method_alias :collection_radio_buttons
173175

174176
def check_boxes_collection(*args)
175177
warn "'BootstrapForm#check_boxes_collection' is deprecated, use 'BootstrapForm#collection_check_boxes' instead"
@@ -218,7 +220,7 @@ def fields_for_with_bootstrap(record_name, record_object = nil, fields_options =
218220
fields_for_without_bootstrap(record_name, record_object, fields_options, &block)
219221
end
220222

221-
alias_method_chain :fields_for, :bootstrap
223+
bootstrap_method_alias :fields_for
222224

223225
private
224226

@@ -276,7 +278,7 @@ def required_attribute?(obj, attribute)
276278

277279
target = (obj.class == Class) ? obj : obj.class
278280

279-
target_validators = if target.respond_to? :validators_on
281+
target_validators = if target.respond_to? :validators_on
280282
target.validators_on(attribute).map(&:class)
281283
else
282284
[]

0 commit comments

Comments
 (0)