Original author is rcknr. I manually transpiled to use JS classes
See here
Otherwise, put this gist into a file called map-label.js and include with import MapLabel from './map-label'. From there, follow the instructions outlined above.
| // Problem: how do you access a deeply nested object that you're not certain exists? | |
| // if(object.user && object.user.post && object.user.post[0] && object.user.post[0].comment) { | |
| // return object.user.post[0].comment | |
| // } | |
| // and that's horrifying. Here's something better | |
| const idx = (props, object) => props.reduce((prefix, val) => (prefix && prefix[val]) ? prefix[val] : null, object) | |
| // Usage: | |
| nestedObj = {...} |
| new RegExp(/\(([\d^)]+)\)(?!\S)/igm) |
| pkill -USR1 puma-dev |
| module HelperProxy | |
| HELPER_DIRECTORY = Dir[Rails.root.join('app', 'helpers', '**', '*.rb')] | |
| HELPER_DIRECTORY.each do |file| | |
| include file.split(/\/helpers\/(.*?)\.rb/)[1].classify.safe_constantize | |
| end | |
| end |
| def assert_differences(expression_array, by:, message: nil, &block) | |
| expressions = Array(expression_array) | |
| change_by = Array(by).map(&:to_i) | |
| if expressions.size != change_by.size | |
| raise ArgumentError, 'Each expression must have a corresponding value to change by' | |
| end | |
| exps = expressions.map.with_index do |e, i| | |
| callable = e.respond_to?(:call) ? e : lambda { eval(e, block.binding) } |
| assert_difference('User.count', -1) do | |
| assert_difference('GoodbyeMailerJob.size', +1) do | |
| assert_difference('Post.count', -5) do | |
| # Logic... | |
| end | |
| end | |
| end |
| assert_differences(['User.count', 'Post.count'], by: [-1, -5]) do | |
| # Logic... | |
| end | |
| assert_differences('User.count', by: -1) do | |
| # Logic... (although you should be using assert_difference in this case) | |
| end | |
| assert_differences(-> { User.count }, by: -1) do | |
| # I can do lambdas too! |
| # Drop into test_helper.rb or similar | |
| def assert_differences(expression_array, by:, message: nil, &block) | |
| expressions = Array(expression_array) | |
| change_by = Array(by).map(&:to_i) | |
| if expressions.size != change_by.size | |
| raise ArgumentError, 'Each expression must have a corresponding value to change by' | |
| end | |
| exps = expressions.map.with_index do |e, i| |
| class Post { // The name of this class doesn't matter, nor does the filepath | |
| constructor() { | |
| // Code related to this class' setup. Not required for Punchbox to run | |
| } | |
| controller() { | |
| // Code within here will run on every action of the controller | |
| } | |
| index() { | |
| // This code will run on the Posts#index action specifically | |
| } |