Skip to content

Instantly share code, notes, and snippets.

View fresh5447's full-sized avatar

Douglas Walter fresh5447

View GitHub Profile
def sort_by_length(sort_this_array)
sort_this_array.sort {|x,y| x.length <=> y.length}
end
def filter(filter_this_array)
filter_this_array.select {|num| num > 5}
end
filter([1, 3, 7, 8])
class Shape
attr_accessor :color
def initialize(color = nil)
@color = color || 'Red'
end
def larger_than?(other)
self.area > other.area
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :name,
has_many :posts
@fresh5447
fresh5447 / gist:10905761
Created April 16, 2014 16:49
Sign out error
<h1>Welcome to Freshitoff</h1>
<p>A simple application for createing To-Do lists.</p>
<div class="btn-group-vertical">
<% @todos.each do |todo| %>
<button type="button" class="btn btn-default"><%= todo.description %></button>
<% end %>
</div>
<ul class="nav nav-pills">
@fresh5447
fresh5447 / gist:9b8adb3e041e8ebd11f9
Created May 1, 2014 20:35
undefined method `each' for nil:NilClass (line 24)
<h1>My wikis</h1>
<div class="row">
<% if policy(Wiki.new).create? %> <%#added policy instead of if user %>
<%= link_to 'New', new_wiki_path, class: 'btn btn-primary btn-large' %>
<% end %>
<% if current_user.role?("free") %>
<h2><%= link_to "Upgrade Account", new_charge_path, class: 'btn btn-primary btn-large' %></h2>
</div>
<% end %>
class WikiPolicy < Struct.new(:user, :wiki)
def update?
if user.role?(:admin)
scope.all
elsif user.role?(:premium)
wiki.user == user || wiki.collaborators.map{|collab| collab.user}.include?(user) || !wiki.private?
else
scope.where(:private => false)
end
@fresh5447
fresh5447 / wikis_controller.rb
Created May 5, 2014 21:07
Unexpected end-of-input, expecting keyword_end
class WikisController < ApplicationController
before_action :set_wiki, only: [:show, :edit, :update, :destroy]
# GET /wikis
# GET /wikis.json
def index
@wikis = policy_scope(Wiki)
end
class WikiPolicy < ApplicationPolicy
class Scope < Struct.new(:user, :scope)
    def resolve
      if user.admin?
        scope.all
      else
        scope.where(user: user)
      end
    end
<h1>My wikis</h1>
<div class="row">
<% if policy(Wiki.new).create? %> <%#added policy instead of if user %>
<%= link_to 'New', new_wiki_path, class: 'btn btn-primary btn-large' %>
<% end %>
<% if current_user.role?("free") %>
<h2><%= link_to "Upgrade Account", new_charge_path, class: 'btn btn-primary btn-large' %></h2>
</div>
<% end %>
class WikiPolicy < ApplicationPolicy
class Scope < Struct.new(:user, :scope)
def resolve
if user.admin?
scope.all
else
scope.where(user: user)
end
end