Skip to content

Instantly share code, notes, and snippets.

View wowremywang's full-sized avatar
🏠
Working from home

wowremywang

🏠
Working from home
View GitHub Profile
@wowremywang
wowremywang / enable-redux-dev-tools.js
Last active January 14, 2021 08:25
Enable Redux Dev Tools
import { createStore, applyMiddleware, compose } from "redux";
import rootReducer from "../reducers/index";
import { forbiddenWordsMiddleware } from "../middleware";
const storeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
rootReducer,
storeEnhancers(applyMiddleware(forbiddenWordsMiddleware))
);
@wowremywang
wowremywang / helpful_methods.md
Last active October 8, 2021 20:02
Helpful methods
each_with_object
## Reference: https://womanonrails.com/each-with-object

# Bad
array = []
(1..10).each do |item|
  array << item ** 2
end
array
@wowremywang
wowremywang / get-unused-routes.rb
Created October 21, 2021 08:09
Get unused routes
Rails.application.eager_load!
unused_routes = {}
# Iterating over all non-empty routes from RouteSet
Rails.application.routes.routes.map(&:requirements).reject(&:empty?).each do |route|
name = route[:controller].camelcase
next if name.start_with?("Rails")
controller = "#{name}Controller"
@wowremywang
wowremywang / avoid-endless-loop.rb
Created October 21, 2021 11:34
Avoid endless loop
# Bad
range = 1..Float::INFINITY
p range.collect { |x| x * x }.first(10)
# Good
range = 1..Float::INFINITY
p range.lazy.collect { |x| x * x }.first(10)
Reference: http://patshaughnessy.net/2013/4/3/ruby-2-0-works-hard-so-you-can-be-lazy
@wowremywang
wowremywang / active-record-queries.rb
Created November 3, 2021 15:10
ActiveRecord Queries
### Rails, ActiveRecord, query id in array of ints, keep order of passed array
# way 1
User.where(id: ids).sort_by { |u| ids.index(u.id) }
# way 2
User.where(id: ids).order("position(id::text in '#{ids.join(',')}')")
@wowremywang
wowremywang / form_helper.rb
Created November 17, 2021 09:23
Form Helpers
# Add data attribute to option
<%= select_tag :addon, options_for_select(@addons.map {|d| [d.name, d.sku, {'data-addon' => d.sku.downcase }]}) %>