Coder Social home page Coder Social logo

rearmed_rails's Introduction

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

Other Libraries in the Rearmed family of Plugins

rearmed_rails's People

Contributors

westonganger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

miclast joeyparis

rearmed_rails's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.