Skip to content

Instantly share code, notes, and snippets.

@Martin91
Martin91 / refinerycms install.sh
Created March 5, 2014 04:22
Install refinerycms from template to be compatible with Rails4
rails new project-name -d mysql -m https://raw.github.com/refinery/refinerycms/master/templates/refinery/edge.rb
@Martin91
Martin91 / append_timestamp.sh
Created May 6, 2014 02:47
Append a timestamp to the file name
rake spiders:prices:update > log/update_prices_$(date +"%Y%m%d%H%M").log
((Date.today - 6.days)..Date.today).map{|date| date.strftime("%m-%d")}
@Martin91
Martin91 / Gemfile
Last active August 29, 2015 14:02 — forked from parndt/gist:1011435
How to cache refinery pages
# Rails4 full page caching is removed and extracted as gem, details from Rails Guides:
# Rails 4.0 has removed Action and Page caching from Action Pack.
# You will need to add the actionpack-action_caching gem in order to use caches_action
# and the actionpack-page_caching to use caches_pages in your controllers.
gem 'actionpack-page_caching'
@Martin91
Martin91 / watch_scss.sh
Created June 10, 2014 16:24
Auto watch changes of scss files which under same directory and generate css files under another directory
#!/bin/bash
# suppose there are two directories named scss and css
sass -t compressed --watch scss:css
@Martin91
Martin91 / state_machine_dynamical_state.rb
Created June 13, 2014 07:45
state_machine dynamical initial state require states list
# From http://stackoverflow.com/a/23497743
state_machine :state, initial: ->(t) { t.active? ? :seeking_flesh : :dormant } do
state :dormant, :seeking_flesh, :attacking # this must be present
end
@Martin91
Martin91 / habtm_save.rb
Created June 23, 2014 16:21
habtm: collection.build doesn't build join associations (unlike collection.create)
@user = User.first
@project = @user.projects.build(:name => "Make your bed")
# if you do @project.save here, as in the gist,
# association will not be saved, but if you do :
@user.save
# it will save the @user with its associated projects
@Martin91
Martin91 / conditions.rb
Created July 19, 2014 01:57
support classed to have abilities to determine if its instances match all or any conditions
module Conditions
def self.included(base)
class << base
attr_reader :conditions_map
def condition(name, &block)
@conditions_map ||= Hash.new
@conditions_map.store(name, block)
end
@Martin91
Martin91 / weibo_oauth2.rb
Last active August 29, 2015 14:04
methods pair to finish OAuth2 flow on weibo.com
# Attention: the below methods are actions extracted from my controllers in a Rails App, if you need to use these codes outside
# a Rails project, rememeber to replace some Rails-specified methods, such as, Hash#to_param.
# STEP 1: start to request an authorization
def request_authorize
session[:security_state] = 'I am from Martin site'
authorize_url = "https://api.weibo.com/oauth2/authorize"
params = {
client_id: "663911642", # Required: Your assigned App Key
@Martin91
Martin91 / state_machine.rb
Last active August 29, 2015 14:04
implement orders' common state machine through state_machine or workflow
# state_machine: https://github.com/pluginaweek/state_machine
class Order < ActiveRecord::Base
state_machine :state, initial: :cart do
before_transition on: :checkout, do: :reduce_buyer_balance
after_transition on: :dispatch_goods, do: :create_shipping_log
after_transition on: :confirm, do: :charge_to_seller
event :checkout do
transition :cart => :paid