Skip to content

Instantly share code, notes, and snippets.

@linyco
linyco / date.js
Created November 7, 2017 17:44
Js - Uib-datepicker date offset by 1 day
var d = new Date('yourDate');
d.setMinutes( d.getMinutes() + d.getTimezoneOffset() );
@linyco
linyco / customer_controller_spec.rb
Last active November 7, 2017 17:27
Rails - Standard Resource Controller
require 'rails_helper'
# The Custom controller is inherited from ResourceController
# and does not have any custom action, which makes it the perfect
# proxy for testing ResourceController
#
describe Admin::CustomersController, type: :controller do
render_views
let!(:customers) { create_list :customer, 3 }
let!(:joe) do
@linyco
linyco / gist:a471daf807d7ead2ca42aa6ea5e2409d
Created October 19, 2017 14:27
Ruby - Pre-commit Rubocop Check
#!/bin/sh
# Put this file into your .git/hooks directory and make it
# executable.
#
# A hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook exits
# with non-zero status if the Rubocop check fails.
# Redirect output to stderr.
@linyco
linyco / toc_generator.rb
Last active October 1, 2017 23:07
Ruby - Generate TOC for md file
# Ref: https://stackoverflow.com/a/22131019/6220029
#!/usr/bin/env ruby
File.open("your_file.md", 'r') do |f|
f.each_line do |line|
forbidden_words = ['Table of contents', 'define', 'pragma']
next if !line.start_with?("#") || forbidden_words.any? { |w| line =~ /#{w}/ }
title = line.gsub("#", "").strip
href = title.gsub(" ", "-").downcase
@linyco
linyco / group_by_count.rb
Created September 7, 2017 22:57
Ruby - count the hash with group_by
x = arr.inject(Hash.new(0)) { |h, e| h[e] += 1 ; h }
@linyco
linyco / query.rb
Last active October 23, 2017 15:06
Rails - search in array column
Price.where("'sat' = ANY (check_in_days)").count
# https://coderwall.com/p/sud9ja/rails-4-the-postgresql-array-data-type
Price.where("array_length(check_in_days, 1) > ?", 0)
# https://www.postgresql.org/docs/9.1/static/functions-array.html
@linyco
linyco / date.js
Last active August 30, 2017 18:38
Js - parse date object from datepicker
// https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results
//
// So, to answer the original asker's question directly,
// "YYYY-MM-DD" is the short form of the ISO 8601 format "YYYY-MM-DDTHH:mm:ss:sssZ".
// So, it is interpreted as UTC time while the other is interpreted as local.
//
// The bottom line is this for parsing date strings.
// The ONLY ISO 8601 string that you can safely parse across browsers is the long form.
// And, ALWAYS use the "Z" specifier.
// If you do that you can safely go back and forth between local and UTC time.
@linyco
linyco / decimal.rb
Last active August 17, 2017 15:57
rails - handling money
# https://stackoverflow.com/questions/1019939/what-is-the-best-method-of-handling-currency-money
# You'll probably want to use a DECIMAL type in your database. In your migration, do something like this:
# precision is the total number of digits
# scale is the number of digits to the right of the decimal point
add_column :items, :price, :decimal, :precision => 8, :scale => 2
# recommended use scale: 2, as we saw in Spree
# when you have to do division, might worth it to do the rounding, float.round(2)
# In Rails, the :decimal type is returned as BigDecimal, which is great for price calculation.
@linyco
linyco / ability_decorator.rb
Last active August 14, 2017 20:43
rails - spree customize ability
class AbilityDecorator
include CanCan::Ability
def initialize(user)
if user.respond_to?(:has_spree_role?) && user.has_spree_role?('agent')
can [:admin, :manage], CreditCard
can [:admin, :manage], Spree::Address
can [:admin, :manage], Spree::Order
can [:admin, :manage], Spree::User
can [:admin, :manage], Spree::Payment
@linyco
linyco / aws_s3_upload.sh
Created July 14, 2017 15:33
Fish - upload local files to s3 bucket
# upload your local files to s3
brew install awscli
aws configure # to add your aws key and secrets
aws s3 cp your_dir s3://bucket_name/folder/ --recursive --acl public-read
https://bucket_name.s3.amazonaws.com/folder/your_file.ext
# ref: http://docs.aws.amazon.com/cli/latest/reference/s3/cp.html