Skip to content

Instantly share code, notes, and snippets.

@mankind
mankind / ability.rb
Created October 11, 2013 13:53
Files with debug code and comments explaining stuff while building my store rails app
class Ability
include CanCan::Ability
def initialize(user)
#to ensure user object is not nil when no user object is passed in
user ||= User.new
can :manage, User, :id => user.id
#can :manage, User do |user|
#thanks to Kevin Wang for posting http://hashrocket.com/blog/posts/bridging-activerecord-and-mongoid
#association_name creates an instance method
#singularize(word) returns the singular form of a word
#classify(table_name) creates a class name from a plural table name eg 'posts.classify' => 'Post'
#underscore(camel_cased_word) Makes an underscored, lowercase form from the expression in the string.
#eg of undrscore -'ActiveModel'.underscore # => "active_model"
#class_eval defines instance methods.
#class_eval evaluates a string/block in the context of the module or class.
#underscore(camel_cased_word) makes an underscore eg 'ActiveModel'.underscore => 'active_model'
@mankind
mankind / courses_controller_test.rb
Created November 4, 2013 11:00
happy path controller test generated by rails scaffold
rails g scaffold course title:string start_day:date start_time:time cost:decimal approved:boolean picture:binary course_duration:integer
require 'test_helper'
class CoursesControllerTest < ActionController::TestCase
setup do
@course = courses(:one)
end
test "should get index" do
@mankind
mankind / rails_nodejs_install.sh
Last active August 29, 2015 14:01
script to install ruby, rails and nodejs. Run the script.
# install using the line below in your terminal
# wget --no-check-certificate https://gist.githubusercontent.com/mankind/3926ea58d2172c260f21/raw/0b426c12c04f75dc78e51e0b556421070801ddae/rails_nodejs_install.sh && bash rails_nodejs_install.sh
echo "======================================="
echo "========= updating Packages repo ========="
echo "======================================="
sudo apt-get -y update && apt-get upgrade
@mankind
mankind / ruby_dependencies.sh
Last active August 29, 2015 14:02
install ruby dependencies
# install using the line below in your terminal
#wget --no-check-certificate https://gist.githubusercontent.com/mankind/f44f8232f5d23a774bae/raw/1f53948207b9ff97cf55954f26b2069096b4d82b/ruby_dependencies.sh && bash ruby_dependencies.sh
echo "======================================="
echo "========= updating Packages repo ========="
echo "======================================="
sudo apt-get -y update && apt-get upgrade
@mankind
mankind / rbenv_install.sh
Last active August 29, 2015 14:02
install rbenv
#https://github.com/fesplugas/rbenv-installer/blob/master/bin/rbenv-installer
#wget --no-check-certificate https://gist.githubusercontent.com/mankind/de9ef556a579f3980dea/raw/301942dcfd869879d9b1f7224d4bb52d78813bec/rbenv_install.sh && bash rbenv_install.sh
sudo apt-get -y update && apt-get upgrade
echo "======================================="
echo "========= Cloning Rbenv ========="
echo "======================================="
# https://gorails.com/setup/ubuntu/13.10
@mankind
mankind / session_controller.rb
Created November 2, 2014 15:25
devise session controller
class SessionsController < Devise::SessionsController
#fix Filter chain halted as :require_no_authentication
prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
#to tackle: Filter chain halted as :verify_signed_out_user
skip_before_filter :verify_signed_out_user, only: :destroy
# disable it for everything except a few methods
#skip_before_action :verify_authenticity_token, :only => :create
@mankind
mankind / controller--application.js
Created November 4, 2014 12:24
ember-cli authentication & user signup
// controllers/application.js
import Ember from 'ember';
export default Ember.Controller.extend({
currentUser: null,
authToken: '',
// used to show, or not show, the log out button
loggedIn: false,
@mankind
mankind / application.js
Last active April 7, 2016 21:58
ember-cli solving rails csrf problems
import Ember from 'ember';
export default Ember.Route.extend({
//a = Charity.__container__.lookup('controller:application')
//a.get('csrfService')
//a.get('csrfService').token.authenticity_token
//or
//a.csrfService
//a.csrfService.token.authenticity_token
@mankind
mankind / route.rb
Created November 4, 2014 14:04
namespaceing devise_for user but not current_user helper
namespace :api do
#when devise_for is called in a namespace, the helpers and controller filters change
#:singular => :user added so we can use current_user instead of current_api_user.
#And before_filter authenticate_user! instead of authenticate_api_user!
#https://github.com/plataformatec/devise/issues/412
#devise_for :users, :singular => :user, skip: :all
get :csrf, to: 'csrf#index'
resources :users