Skip to content

Instantly share code, notes, and snippets.

View mkwiatkowski's full-sized avatar

Michal Kwiatkowski mkwiatkowski

View GitHub Profile
class Thing
def description
# An object can always access its methods, whether they are public,
# protected or private.
"#{shape} thing in #{color}"
end
def combined_description(other)
# An object can access methods of other objects of the same class if they
# are public or protected.
begin;
/*
Problem: I don't want the application to really care about how the data is
structured in the database. The application / view code needs to work
with the data in a sane format, without worrying about normalization, joins,
eager fetching, etc.
DB Schema Overview:
orders
[2] pry(main)> Bookmark.favorited(User.first)
User Load (0.2ms) SELECT "users".* FROM "users" LIMIT 1
Bookmark Load (0.2ms) SELECT "bookmarks".* FROM "bookmarks" INNER JOIN "favorites" ON "favorites"."bookmark_id" = "bookmarks"."id" WHERE "bookmarks"."user_id" = 1 ORDER BY created_at Desc
=> []
[3] pry(main)> current_user = User.first
User Load (0.2ms) SELECT "users".* FROM "users" LIMIT 1
=> #<User id: 1, email: "tyrese@faheywill.info", password_hash: "$2a$10$G47zK5tFMC9KnjZeRotQL.QsevYfpgX9C2cGbWzmY7hS...", password_salt: "$2a$10$G47zK5tFMC9KnjZeRotQL.", created_at: "2014-01-21 07:22:35", updated_at: "2014-01-21 07:22:35", name: "Jeffry Kuhic MD", role: nil>
[4] pry(main)> @bookmark = Bookmark.find(1)
Bookmark Load (21.6ms) SELECT "bookmarks".* FROM "bookmarks" WHERE "bookmarks"."id" = ? ORDER BY created_at Desc LIMIT 1 [["id", 1]]
=> #<Bookmark id: 1, url: "http://hoeger.biz/rachel_orn", title: "reprehenderit et", description: "Vel sunt reprehenderit amet. Id est quia amet digni...", created_a
# application_helper.rb
def bookmark_thumbnail(metadata)
image_tag(metadata[:thumbnail_url] || "thumb.png", width: "260")
end
# migration
add_column :bookmarks, :metadata, :text
# bookmark model
after_save :get_embedly_data
diff --git a/app/controllers/hashtags_controller.rb b/app/controllers/hashtags_controller.rb
index f67091b..29d121a 100644
--- a/app/controllers/hashtags_controller.rb
+++ b/app/controllers/hashtags_controller.rb
@@ -1,7 +1,6 @@
class HashtagsController < ApplicationController
def index
- @hashtags = Hashtag.all
- @hashtag_groups = @hashtags.group_by { |t| t.tag }
+ @hashtags = Hashtag.all
@mkwiatkowski
mkwiatkowski / Workshop-cheatsheet.md
Last active January 3, 2016 08:49
Bloc workshop cheatsheet
diff --git a/app/policies/topic_policy.rb b/app/policies/topic_policy.rb
index c247c17..2b4bd00 100644
--- a/app/policies/topic_policy.rb
+++ b/app/policies/topic_policy.rb
@@ -11,7 +11,7 @@ class TopicPolicy < ApplicationPolicy
user.present? && user.role?(:admin)
end
- def public?
- Topic.all.visible_to(user)
@mkwiatkowski
mkwiatkowski / gist:8067287
Created December 21, 2013 09:30
Annotated ability.rb file
class Ability # Defining a class that will be recognized and used by the CanCan gem.
include CanCan::Ability # Including CanCan::Ability module pulls in the "can" method, so it can be used in the methods of this class.
def initialize(user) # By CanCan convention, currently logged in user (also accessible with "current_user" method in controllers and views) is passed to the initializer.
user ||= User.new # If "current_user" was nil, this will assign a new User object to the "user" variable. New users have no "role" attribute set (i.e. role = nil).
if user.role? :member # Calling "role?" method on user to see if a user has member privileges.
can :manage, Post, :user_id => user.id # To understand the "can" method it's best to read this wiki page: https://github.com/ryanb/cancan/wiki/defining-abilities
can :manage, Comment, :user_id => user.id # In short, this line will allow the user to manage (i.e. do anything with) comments created by her. That's because comments created by giv
$ irb
2.0.0p247 :001 > def link_to(text, address) # <= pasting method definition
2.0.0p247 :002?> p "<a href='#{address}'>#{text}</a>"
2.0.0p247 :003?> end
=> nil
2.0.0p247 :004 > link_to("Bloc", "http://www.bloc.io") # <= manually invoking the method with some arguments
"<a href='http://www.bloc.io'>Bloc</a>" # <= what your method printed
=> "<a href='http://www.bloc.io'>Bloc</a>" # <= what your method returned
# Connect to the database.
db = Mongo::MongoClient.new.db("database")
begin
# Do an operation within 1 second limit.
Timeout.timeout(1) do
db["collection"].insert({'attribute' => 'value'})
end
rescue Timeout::Error
# Do something else in case of a timeout.