Skip to content

Instantly share code, notes, and snippets.

View vmcilwain's full-sized avatar

Vell vmcilwain

View GitHub Profile
@vmcilwain
vmcilwain / trix_rails_from_tag
Created October 18, 2017 17:19
Adding trix editor to a rails form_tag (using haml)
# Given there is a section object and I would like to use trix editor for adding text to the content attribute
# id: is required to tie trix-editor(below to to the hidden input
# name: is required so that you can find it via params in the controller in this case params["trix-editor_section_#{section.id}"]
# content: is for inserting any content that already existed in the content attribute
%input{id: "#{dom_id(section, 'trix-editor')}", name: "#{dom_id(section, 'trix-editor')}", content: "#{section.content}", type: :hidden}
%trix-editor{input: "#{dom_id(section, 'trix-editor')}"}
@vmcilwain
vmcilwain / icheck_checkboxes.coffee
Last active November 1, 2018 11:44
Processing iCheck checkboxes
# Code for processing icheck checkboxes
# with slight modification it can be used with normal checkboxes as well
# Check or uncheck all checkboxes
#
$('#checkAll').on 'ifChecked ifUnchecked', (event) ->
$checkboxes = $(this).closest('form').find(':checkbox')
if event.type == 'ifChecked'
$checkboxes.prop 'checked', true
@vmcilwain
vmcilwain / disable_links.coffee
Created May 29, 2018 13:38
Disable links with a data attribute named action that ends with `_export_action`
@vmcilwain
vmcilwain / main_app_example.rb
Last active June 4, 2018 14:03
Rails 4.2.0 Engine RSpec main app routing
# spec/support/main_app.rb
module MainAppRoute
def main_app
Rails.application.class.routes.url_helpers
end
end
# In spec/rails_helper
RSpec.config do |config|
config.include MainAppRoute
@vmcilwain
vmcilwain / string_to_boolean.rb
Created June 14, 2018 14:12
Rails 3 casting a string value into a boolean
ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
# Values for true:
# 1, "1", "t", "T", "true", "TRUE", true, "on", "ON"
# Values for false:
# 0, "0", "n", "N", "false", "FALSE", false, "off", "OFF"
@vmcilwain
vmcilwain / scan_and_verify.rb
Last active February 5, 2019 15:23
[Ruby] Email address scanner and verify
# scan for email addresses
text-variable.scan(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/i)
=> ['[email protected]', 'user2@domain']
# Verify email addresses
a = Mail::Address.new('Foo Bar <[email protected]>')
a.address
=> "[email protected]"
@vmcilwain
vmcilwain / serialize_strong_parameter.rb
Last active August 22, 2019 14:30
[Rails] Add serialized data to strong parameters
# Where data is a hash of predefined keys
# This example also shows at the end another hash inside of data
params.permit(
:date_range,
:email,
:enabled,
:id,
:limit,
:match,
:send_day,
@vmcilwain
vmcilwain / icheck_helpers.rb
Last active December 13, 2018 16:28
[Capybara] iCheck checkbox helper methods (using JQuery)
# iCheck helper methods
def check_checkbox(attribute=:name, value)
evaluate_script "$(\":checkbox[#{attribute}='#{value}']\").prop('checked', true).iCheck('update');"
end
def uncheck_checkbox(attribute=:name, value)
evaluate_script "$(\":checkbox[#{attribute}='#{value}']\").prop('checked', false).iCheck('update');"
end
# above can be refactored_to
@vmcilwain
vmcilwain / submit-checkbox-as-hash.rb
Last active November 7, 2018 18:05
[Rails] Submit multiple checkboxes as a hash :id => boolean
# In the rails controller something like:
def _params
params.permit(:param1, :param2).tap do |whitelisted|
# because submitting objects is optional
(whitelisted[:objects] = params[:objects]) if params[:objects]
end
end
# summary_params['objects'] => {'object.id' => 'boolean-valu'}
@vmcilwain
vmcilwain / rails_6_0_alpha.txt
Last active November 26, 2021 21:42
Steps to install rails 6.0 alpha
Pre reqs:
I installed these using homebrew
* nodejs
* yarn
Create an application directory manually
$ mkdir app_name