All Projects → westonganger → rearmed_rails

westonganger / rearmed_rails

Licence: MIT License
A collection of helpful methods and monkey patches for Rails

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to rearmed rails

MentalGL
Single header OpenGL utility library in the public domain
Stars: ✭ 20 (-41.18%)
Mutual labels:  utility
bash
A collection of small bash utils.
Stars: ✭ 15 (-55.88%)
Mutual labels:  utility
CLIp
CLIp is a clipboard manager for a command line interface written in 100% standard C only. Pipe to it to copy, pipe from it to paste.
Stars: ✭ 12 (-64.71%)
Mutual labels:  utility
Pagination-Utils
A collection of methods to make message pagination with JDA easier.
Stars: ✭ 20 (-41.18%)
Mutual labels:  utility
pynotify
A Python package to send emails like humans.
Stars: ✭ 21 (-38.24%)
Mutual labels:  utility
tiler
N-dimensional NumPy array tiling and merging with overlapping, padding and tapering
Stars: ✭ 26 (-23.53%)
Mutual labels:  utility
pv
Unix Pipe Viewer (pv) utility in Node.js
Stars: ✭ 20 (-41.18%)
Mutual labels:  utility
ctxutil
utils for Go context
Stars: ✭ 18 (-47.06%)
Mutual labels:  utility
diskusage
FANTASTIC SPEED utility to find out top largest folders/files on the disk.
Stars: ✭ 64 (+88.24%)
Mutual labels:  utility
asynckit
Minimal async jobs utility library, with streams support
Stars: ✭ 21 (-38.24%)
Mutual labels:  utility
xbytes
Parse bytes to human readable sizes (4747) → ('4.75 KB') and vice versa.
Stars: ✭ 17 (-50%)
Mutual labels:  utility
pyGroff
laTEX is awesome but we are lazy -> groff with markdown syntax and inline code execution
Stars: ✭ 25 (-26.47%)
Mutual labels:  utility
findlargedir
find all "blackhole" directories with a huge amount of filesystem entries in a flat structure
Stars: ✭ 15 (-55.88%)
Mutual labels:  utility
YetAnotherKeyDisplayer
The application for displaying pressed keys of the keyboard
Stars: ✭ 88 (+158.82%)
Mutual labels:  utility
Vutils
Vutils or Vic Utilities is an utility library written in Modern C++ and for Modern C++. It helps your programming go easier, faster, and simpler.
Stars: ✭ 16 (-52.94%)
Mutual labels:  utility
Resource Monitor
Resource_Monitor is a GNOME Shell extension that Monitor the use of system resources like cpu, ram, disk, network and display them in GNOME Shell top bar.
Stars: ✭ 62 (+82.35%)
Mutual labels:  utility
91porn-utility
91porn comprehensive utility
Stars: ✭ 78 (+129.41%)
Mutual labels:  utility
dePAC
seamless Proxy Auto-Config (a.k.a. Web Proxy Auto Discovery) for CLI apps
Stars: ✭ 26 (-23.53%)
Mutual labels:  utility
ToGoZip
Android share/sendTo menu implementation "add2Zip"
Stars: ✭ 44 (+29.41%)
Mutual labels:  utility
json2xml
json to xml converter in python3
Stars: ✭ 76 (+123.53%)
Mutual labels:  utility

Rearmed Rails

Gem Version CI Status RubyGems Downloads Buy Me a Coffee

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.

# Gemfile

gem '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.

# config/initializers/rearmed.rb

RearmedRails.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:

### 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

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 on

Post.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 fired

Post.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 postgresql

Post.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

# 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])

# 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 - @westonganger

For any consulting or contract work please contact me via my company website: Solid Foundation Web Development

Other Libraries in the Rearmed family of Plugins

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].