Skip to content

Instantly share code, notes, and snippets.

@ayamomiji
ayamomiji / gist:4736614
Last active November 10, 2017 11:16
turbolinks + angularjs
bootstrapAngular = ->
$('[ng-app]').each ->
module = $(this).attr('ng-app')
angular.bootstrap(this, [module])
$(document).on('page:load', bootstrapAngular)
(scope, elem, attrs) ->
id = _.uniqueId()
elem.attr('endless-page-elem-id', id)
$(window).on 'scroll', ->
return if $("[endless-page-elem-id='#{id}']").length is 0
...
class AddHstoreExtension < ActiveRecord::Migration
def up
enable_extension 'hstore'
end
def down
disable_extension 'hstore'
end
end
@ayamomiji
ayamomiji / module.js.coffee
Created December 4, 2013 16:36
add csrf token to angular http requests
module = angular.module(...)
module.config ($httpProvider) ->
authToken = $('meta[name="csrf-token"]').attr('content')
$httpProvider.defaults.headers.common['X-CSRF-TOKEN'] = authToken
@ayamomiji
ayamomiji / module.js.coffee
Created December 4, 2013 16:40
make $http to support patch method
module = angular.module(...)
module.config ($httpProvider, $provide) ->
$httpProvider.defaults.headers.patch = {'Content-Type': 'application/json'}
$provide.decorator '$http', ($delegate) ->
$delegate.patch = (url, config) ->
$delegate(angular.extend(config or {}, method: 'patch', url: url))
$delegate
@ayamomiji
ayamomiji / slim_template.rb
Created December 4, 2013 16:47
use slim in sprockets
class CustomSlimEngine < Slim::Template
def evaluate(scope, locals, &block)
scope.class_eval do
include Rails.application.routes.url_helpers
include Rails.application.routes.mounted_helpers
include ActionView::Helpers
end
super
end
require 'rack/rewrite'
Rails.application.config.middleware.insert(0, Rack::Rewrite) do
rewrite %r{^/(?!assets|auth|admin)}, '/'
end
@ayamomiji
ayamomiji / deploy.rb
Last active October 19, 2021 18:12
capistrano 3: precompile assets on local machine then upload
namespace :deploy do
namespace :assets do
Rake::Task['deploy:assets:precompile'].clear_actions
desc "Precompile assets on local machine and upload them to the server."
task :precompile do
run_locally do
execute 'RAILS_ENV=production bundle exec rake assets:precompile'
end
Rails.application.config.assets.precompile += %w( ratchicons.eot
ratchicons.woff
ratchicons.ttf
ratchicons.svg )
# 不考慮重複 key 和無 value 的情況
parseQS = (qs = location.search) ->
object = {}
pairs = qs.substring(1) # 去除前置 ? 字元
.split('&') # 變成一堆 oo=xx 字串
for pair in pairs
[key, value] = pair.split('=')
object[key] = value