Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/westonganger/rearmed_rails
A collection of helpful methods and monkey patches for Rails
https://github.com/westonganger/rearmed_rails
monkey-patching rails ruby utility
Last synced: about 1 month ago
JSON representation
A collection of helpful methods and monkey patches for Rails
- Host: GitHub
- URL: https://github.com/westonganger/rearmed_rails
- Owner: westonganger
- License: mit
- Archived: true
- Created: 2017-02-28T16:23:19.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-05-18T17:21:35.000Z (over 1 year ago)
- Last Synced: 2024-09-26T16:38:02.094Z (about 1 month ago)
- Topics: monkey-patching, rails, ruby, utility
- Language: Ruby
- Size: 43 KB
- Stars: 33
- Watchers: 4
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Rearmed Rails
A collection of helpful methods and monkey patches for Rails
The difference between this library and others is that all monkey patching is performed in an opt-in way because you shouldnt be using methods that you dont know about.
```ruby
# Gemfilegem 'rearmed_rails'
```Run `rails g rearmed_rails:setup` to create a settings files in `config/initializers/rearmed_rails.rb` where you can opt-in to the monkey patches available in the library. Set these values to true if you want to enable the applicable monkey patch.
```ruby
# config/initializers/rearmed.rbRearmedRails.enabled_patches = {
active_record: {
find_duplicates: false,
find_or_create: false,
newest: false,
pluck_to_hash: false,
pluck_to_struct: false,
reset_auto_increment: false,
reset_table: false
},
helpers: {
field_is_array: false,
link_to_confirm: false,
options_for_select_include_blank: false,
options_from_collection_for_select_include_blank: false
}
}RearmedRails.apply_patches!
```Some other argument formats the `enabled_patches` option accepts are:
```ruby
### Enable everything
RearmedRails.enabled_patches = :all### Disable everything
RearmedRails.enabled_patches = nil### Hash values can be boolean/nil values also
RearmedRails.enabled_patches = {
active_record: true,
helpers: false,
}
```By design, once `apply_patches!` is called then `RearmedRails.enabled_patches` is no longer editable and `apply_patches!` cannot be called again. If you try to do so, it will raise a `PatchesAlreadyAppliedError`. There is no-built in way of changing the patches, if you need to do so (which you shouldn't) that is up to you to figure out.
## Rails
### ActiveRecord
```ruby
Post.find_or_create(name: 'foo', content: 'bar') # use this instead of the super confusing first_or_create method
Post.find_or_create!(name: 'foo', content: 'bar')Post.newest # get the newest post, by default ordered by :created_at
Post.newest(:updated_at) # different sort order
Post.newest(:published_at, :created_at) # multiple columns to sort onPost.pluck_to_hash(:name, :category, :id)
Post.pluck_to_struct(:name, :category, :id)Post.reset_table # delete all records from table and reset autoincrement column (id), works with mysql/mariadb/postgresql/sqlite
# or with options
Post.reset_table(delete_method: :destroy) # to ensure all callbacks are firedPost.reset_auto_increment # reset mysql/mariadb/postgresql/sqlite auto-increment column, if contains records then defaults to starting from next available number
# or with options
Post.reset_auto_increment(value: 1, column: :id) # column option is only relevant for postgresqlPost.find_duplicates # return active record relation of all records that have duplicates. By default it skips the primary_key, created_at, updated_at, & deleted_at columns
Post.find_duplicates(:name) # find duplicates based on the name attribute
Post.find_duplicates(:name, :category) # find duplicates based on the name & category attribute
Post.find_duplicates(self.column_names.reject{|x| ['id','created_at','updated_at','deleted_at'].include?(x)})# It also can delete duplicates.
# Valid values for keep are :first & :last.
# Valid values for delete_method are :destroy & :delete. The soft-delete option is only used if you are using acts_as_paranoid on your model.
Post.find_duplicates(:name, :category, delete: true)
Post.find_duplicates(:name, :category, delete: {keep: :first, delete_method: :destroy, soft_delete: true}) # these are the default settings for delete: true
```### Helpers
```ruby
# field_is_array: works with field type tag, form_for, simple form, etc
= text_field_tag :name, is_array: true #=># 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])# returns Rails v3 behaviour of allowing confirm attribute as well as data-confirm
= link_to 'Delete', post_path(post), method: :delete, confirm: "Are you sure you want to delete this post?"
```# Contributing
If you want to request a new method please raise an issue and we will discuss the idea.# Credits
Created by [Weston Ganger](https://westonganger.com) - [@westonganger](https://github.com/westonganger)## Other Libraries in the Rearmed family of Plugins
- [Rearmed Ruby](https://github.com/westonganger/rearmed-rb)
- [Rearmed JS](https://github.com/westonganger/rearmed_rails)
- [Rearmed CSS](https://github.com/westonganger/rearmed_css)