Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
namespace rails patches, add rails other patches
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Feb 19, 2017
1 parent b4132cb commit 205624e
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 29 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
CHANGELOG
---------

- **1.2.2 - Feb 19, 2017**
- Add `field_is_array`, `options_for_select_include_blank`, `options_from_collect_for_select_include_blank`
- Namespace rails patches to active_record and other
- **1.2.1 - Jan 24, 2017**
- Add Hash `join`, `to_struct`
- Fix `find_duplicates`
Expand Down
38 changes: 29 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,22 @@ Rearmed.enabled_patches = {
assert_not_changed: false
},
rails: {
find_duplicates: false,
find_in_relation_batches: false,
find_or_create: false,
find_relation_each: false,
newest: false,
pluck_to_hash: false,
pluck_to_struct: false,
reset_auto_increment: false,
reset_table: false
active_record: {
find_duplicates: false,
find_in_relation_batches: false,
find_or_create: false,
find_relation_each: false,
newest: false,
pluck_to_hash: false,
pluck_to_struct: false,
reset_auto_increment: false,
reset_table: false
},
other: {
field_is_array: false,
options_for_select_include_blank: false,
options_from_collection_for_select_include_blank: false
}
},
rails_4: {
link_to_confirm: false,
Expand Down Expand Up @@ -214,6 +221,19 @@ Post.find_in_relation_batches # this returns a relation instead of an array
Post.find_relation_each # this returns a relation instead of an array
```

##### Other

```ruby
# field_is_array: works with field type tag, form_for, simple form, etc
= text_field_tag :name, is_array: true #=> <input type='text' name='name[]' />

# options_for_select_include_blank
options_for_select(@users.map{|x| [x.name, x.id]}, include_blank: true, selected: params[:user_id])

# options_from_collection_for_select_include_blank
options_from_collection_for_select(@users, 'id', 'name', include_blank: true, selected: params[:user_id])
```

##### Rails 4.x Backports
```ruby
# returns to rails 3 behaviour of allowing confirm attribute as well as data-confirm
Expand Down
25 changes: 16 additions & 9 deletions lib/rearmed/default_enabled_patches.hash
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,22 @@
assert_not_changed: false
},
rails: {
find_duplicates: false,
find_in_relation_batches: false,
find_or_create: false,
find_relation_each: false,
newest: false,
pluck_to_hash: false,
pluck_to_struct: false,
reset_auto_increment: false,
reset_table: false
active_record: {
find_duplicates: false,
find_in_relation_batches: false,
find_or_create: false,
find_relation_each: false,
newest: false,
pluck_to_hash: false,
pluck_to_struct: false,
reset_auto_increment: false,
reset_table: false
},
other: {
field_is_array: false,
options_for_select_include_blank: false,
options_from_collection_for_select_include_blank: false
}
},
rails_4: {
link_to_confirm: false,
Expand Down
92 changes: 82 additions & 10 deletions lib/rearmed/monkey_patches/rails.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
enabled = Rearmed.enabled_patches[:rails] == true

if defined?(ActiveRecord)
ar_enabled = enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :active_record) == true

ActiveRecord::Base.class_eval do
if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :newest)
if ar_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :active_record, :newest)
def self.newest(*columns)
if columns.empty? || !columns.to_s.include?('created_at')
columns << 'created_at'
Expand All @@ -17,7 +18,7 @@ def self.newest(*columns)
end
end

if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :reset_table)
if ar_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :active_record, :reset_table)
def self.reset_table(opts={})
if opts[:delete_method] && opts[:delete_method].to_sym == :destroy
if self.try(:paranoid?)
Expand Down Expand Up @@ -52,7 +53,7 @@ def self.reset_table(opts={})
end
end

if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :reset_auto_increment)
if ar_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :active_record, :reset_auto_increment)
def self.reset_auto_increment(opts={})
case self.connection.adapter_name.downcase.to_sym
when :mysql2
Expand All @@ -68,7 +69,7 @@ def self.reset_auto_increment(opts={})
end
end

if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :find_duplicates)
if ar_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :active_record, :find_duplicates)
def self.find_duplicates(*args)
options = {}

Expand Down Expand Up @@ -142,7 +143,7 @@ def self.find_duplicates(*args)
end
end

if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :find_or_create)
if ar_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :active_record, :find_or_create)
def self.find_or_create(attrs={}, save_opts={})
unless self.where(attrs).limit(1).first
x = self.class.new(attrs)
Expand All @@ -160,7 +161,7 @@ def self.find_or_create!(attrs={}, save_opts={})
end
end

if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :pluck_to_hash)
if ar_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :active_record, :pluck_to_hash)
def self.pluck_to_hash(*keys)
hash_type = keys[-1].is_a?(Hash) ? keys.pop.fetch(:hash_type, HashWithIndifferentAccess) : HashWithIndifferentAccess
block_given = block_given?
Expand All @@ -174,7 +175,7 @@ def self.pluck_to_hash(*keys)
end
end

if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :pluck_to_struct)
if enabled || ar_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :active_record, :pluck_to_struct)
def self.pluck_to_struct(*keys)
struct_type = keys[-1].is_a?(Hash) ? keys.pop.fetch(:struct_type, Struct) : Struct
block_given = block_given?
Expand All @@ -191,7 +192,7 @@ def self.pluck_to_struct(*keys)

private

if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :pluck_to_hash) || Rearmed.dig(Rearmed.enabled_patches, :rails, :pluck_to_struct)
if ar_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :active_record, :pluck_to_hash) || Rearmed.dig(Rearmed.enabled_patches, :rails, :pluck_to_struct)
def self.format_keys(keys)
if keys.blank?
[column_names, column_names]
Expand All @@ -213,7 +214,7 @@ def self.format_keys(keys)
end

ActiveRecord::Batches.module_eval do
if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :find_in_relation_batches)
if ar_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :active_record, :find_in_relation_batches)
def find_in_relation_batches(options = {})
options.assert_valid_keys(:start, :batch_size)

Expand Down Expand Up @@ -250,7 +251,7 @@ def find_in_relation_batches(options = {})
end
end

if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :find_relation_each)
if ar_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :active_record, :active_record, :find_relation_each)
def find_relation_each(options = {})
if block_given?
find_in_relation_batches(options) do |records|
Expand All @@ -266,3 +267,74 @@ def find_relation_each(options = {})
end

end

if defined?(ActionView::Helpers)
other_enabled = enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :other) == true

ActionView::Helpers.module_eval do

if other_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :other, :options_for_select_include_blank)
def options_for_select(container, selected = nil)
return container if String === container

if selected.is_a?(Hash)
include_blank = selected[:include_blank] || selected['include_blank']
end

selected, disabled = extract_selected_and_disabled(selected).map do |r|
Array(r).map(&:to_s)
end

options = []

options.push([nil,nil]) if include_blank

container.each do |element|
html_attributes = option_html_attributes(element)
text, value = option_text_and_value(element).map(&:to_s)

html_attributes[:selected] ||= option_value_selected?(value, selected)
html_attributes[:disabled] ||= disabled && option_value_selected?(value, disabled)
html_attributes[:value] = value

options.push content_tag_string(:option, text, html_attributes)
end

options.join("\n").html_safe
end
end

if other_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :other, :options_from_collection_for_select_include_blank)
def options_from_collection_for_select(collection, value_method, text_method, selected = nil)
options = collection.map do |element|
[value_for_collection(element, text_method), value_for_collection(element, value_method), option_html_attributes(element)]
end

if selected.is_a?(Hash)
include_blank = selected[:include_blank] || selected['include_blank']
end

selected, disabled = extract_selected_and_disabled(selected)

select_deselect = {
selected: extract_values_from_collection(collection, value_method, selected),
disabled: extract_values_from_collection(collection, value_method, disabled),
include_blank: include_blank
}

options_for_select(options, select_deselect)
end
end

if other_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :other, :field_is_array)
original_method = instance_method(:add_default_name_and_id)
define_method :add_default_name_and_id do |options|
if options['is_array'] && options['name']
options['name'] = "#{options['name']}[]"
end
original_method.bind(self).(options)
end
end

end
end
2 changes: 1 addition & 1 deletion lib/rearmed/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Rearmed
VERSION = "1.2.1"
VERSION = "1.2.2"
end

0 comments on commit 205624e

Please sign in to comment.