Skip to content

Instantly share code, notes, and snippets.

View adamkleingit's full-sized avatar

Adam Klein adamkleingit

View GitHub Profile
@adamkleingit
adamkleingit / sti_routes.rb
Created June 17, 2013 20:52
Routes for STI models
# If you want url_for(CoolProject) to give you 'cool_projects/1' but still go to ProjectsController then:
resources :cool_projects, :controller => :projects
# If you want url_for(CoolProject) to give you 'projects/1' and also go to ProjectsController then:
resources :cool_projects, :controller => :projects, :as => :projects
# If you want to reuse stuff inside resources:
projects_inner = lambda do
member do
resources :submodels
@adamkleingit
adamkleingit / method_only_in_parent.rb
Last active December 18, 2015 15:59
Define secret methods only in parent
module SecretMethods
def secret_def(name, &block)
klass = self
define_method name do
if self.class != klass
raise NoMethodError
else
yield
end
end
@adamkleingit
adamkleingit / user.rb
Last active December 20, 2015 08:08
500Tech / Rails Riddles #2
# Improve / Fix as many things in the code below
# Answers should be sent to hackademy@500tech.com
# Good luck
class User < ActiveRecord::Base
validates_presence_of :name, :email, :date_of_birth, :age_category
before_save do
age = (Time.now - date_of_birth).year
if age < 10 age_category = 'child'
@adamkleingit
adamkleingit / homework.md
Last active January 3, 2016 19:59
Homework for lesson #5

Homework:

  • implement the missing stuff from presentation:
    • flash messages
    • before filters
    • simple_form
  • Create a cat model:
    • name (string) - required
    • bio (text) - optional
  • gender (string) - Male or Female
@adamkleingit
adamkleingit / bardelas_homework_6.md
Last active January 4, 2016 06:19
Hackademy Bardelas Homework lesson #6
  • devise and authentication:
    • Change after_sign_in path
    • Change some devise config
    • Change devise forgot password email's content
  • associations:
    • Play with other dependents
    • Name the user's association 'owner' (comment out the original one so you can revert)
    • Name the cat's association 'owned_cats' (comment out the original one so you can revert)
  • authorization:
  • restrict users from editing / updating other users
@adamkleingit
adamkleingit / lesson_7.md
Last active January 4, 2016 16:39
Lesson 7 homework
  • Project cleanup
    • Remove users new, create, edit, update, destroy actions and relevant links
    • Add link to edit_user_registration_path from the user's email
    • Make sure has_many :cats uses dependent destroy
  • destroy
    • Don't allow destroying the last user in the DB
    • Don't allow destroying female cats
    • Use can_edit? to show / hide destroy link for cat
  • many to many
  • Add validation on like model - user can't like a cat twice
@adamkleingit
adamkleingit / gist:0a08c18d77cf5e8e0e01
Created November 26, 2014 09:12
Closures for Angular controllers
var MyCtrl = (function () {
var myDependency;
function MyCtrl(_myDependency_){
myDependency = _myDependency_;
}
MyCtrl.prototype.myFunction = function () {
return myDependency.aFunction();
}
return MyCtrl;
})()
@adamkleingit
adamkleingit / hover to expand input.css
Last active August 29, 2015 14:22
hover to expand input
input {
overflow:hidden;
width:32px;
height:30px;
border-color:grey;
transition:width 0.4s linear, border-color 0.4s linear;
&.hover {
width:236px;
}
}
@adamkleingit
adamkleingit / DashboardCtrl.js
Created August 20, 2015 14:55
Example of changing URL without refreshing the view
function DashboardCtrl(DashboardFilter, pubsub) {
var self = this;
function init() {
// Bindable filter, updates automatically
self.filter = DashboardFilter.getFilter();
// Listen to filter change:
pubsub.on(DASHBOARD_FILTER_CHANGE, function(oldFilter, newFilter) {
// Example handling of filter change:
if (oldFilter.dashboardName != newFilter.dashboardName) {
self.initPanes();
@adamkleingit
adamkleingit / chaining.js
Last active September 28, 2015 08:34
example of using defer
function getAllUsers() {
let promise = $http.get('/settings')
.then((settings) => {
if(settings.defaultUsers) {
return getDefaultUsers();
}
else {
// get some more data
return $http.get('/users')
.then((users) => {