Skip to content

Instantly share code, notes, and snippets.

@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 / 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
class AddHstoreExtension < ActiveRecord::Migration
def up
enable_extension 'hstore'
end
def down
disable_extension 'hstore'
end
end
(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
...
@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)
def whatever
blabla
render nothing: true
end
@ayamomiji
ayamomiji / first.rb
Created May 15, 2012 07:33
resource loading patterns
class PostsController
before_filter :load_user
before_filter :load_post
def show
respond_with(@post)
end
def load_user
@user = User.find(params[:user_id])
@ayamomiji
ayamomiji / rest.rb
Created April 27, 2012 07:55
rest http client idea
# client usage
client.resources('/articles').post # => POST /articles
client.articles.post # => POST /articles
client.article(1).get # => GET /articles/1
client.article(1).comments.get # => GET /articles/1/comments
client.admin # => client with admin support (for example, use http basic auth middleware)
client.admin.articles.get # => GET /admin/articles with http basic auth
# client define
@ayamomiji
ayamomiji / json_column.rb
Created April 17, 2012 17:42
rails serialize column in json
class JsonColumn
def self.default_with(&block)
new(block)
end
def initialize(default)
@default = default
end
def dump(obj)
@ayamomiji
ayamomiji / net-http.rb
Created April 17, 2012 14:06
rest-core/app/net-http.rb
require 'rest-core/middleware'
require 'net/http'
class RestCore::NetHttp
include RestCore::Middleware
def call env
uri = URI.parse(request_uri(env))
payload = ::RestClient::Payload.generate(env[REQUEST_PAYLOAD])
client = Net::HTTP.new(uri.host, uri.port)