This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class User < ActiveRecord::Base | |
| def admin? | |
| role? :admin | |
| end | |
| def role?(role) | |
| roles.include? role | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module BookScopes | |
| def self.extended(base) | |
| base.instance_exec &scope_definitions | |
| end | |
| def self.included(base) | |
| base.class_exec &scope_definitions | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BooksController < ApplicationController | |
| def index | |
| # params = { filters: { genre: "Westerns", sold_out: 1 } } | |
| @books = Book.filtered(params[:filters]) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module ActsAsClassMethod | |
| unless respond_to? :metaclass | |
| define_method :metaclass do | |
| class << self; self; end | |
| end | |
| end | |
| def acts_as_class_method(*methods) | |
| methods.each do |method| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module HasStatuses | |
| def has_statuses(*statuses) | |
| self.const_set "Statuses", Module.new | |
| statuses.each { |status| self::Statuses.const_set status.to_s.upcase, status } | |
| self.const_set "ALL_STATUSES", self::Statuses.constants.collect { |constant| self::Statuses.const_get(constant) } | |
| end | |
| Object.send :extend, self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module WhereOr | |
| def where_or(*scopes) | |
| conditions = scopes.inject(self) { |scoped, scope| scoped.send(scope) } | |
| conditions = conditions.where_values.map { |where_value| where_value.respond_to?(:to_sql) ? where_value.to_sql : where_value } | |
| conditions = conditions.join(" OR ") | |
| where(conditions) | |
| end | |
| ActiveRecord::Base.send :extend, self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "active_support/inflector" | |
| module Modularizable | |
| def modularize(target, params={}) | |
| ancestors = params[:in] || [] | |
| ancestors << target | |
| ancestors.collect { |item| item.to_s.camelize }.join("::").constantize | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Paginator | |
| DEFAULT_PAGE = 1 | |
| DEFAULT_PER_PAGE = 5 | |
| attr_reader :page | |
| attr_reader :per_page | |
| def paginate(collection, options={}) | |
| setup(options) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Article.all( | |
| :select => "articles.id, articles.title, slugs.name", | |
| :joins => "INNER JOIN slugs | |
| ON slugs.sluggable_type = 'Article' AND | |
| slugs.sluggable_id = articles.id AND | |
| slugs.id = (SELECT id | |
| FROM slugs | |
| WHERE slugs.sluggable_type = 'Article' AND | |
| slugs.sluggable_id = articles.id | |
| ORDER BY slugs.created_at DESC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace :deploy do | |
| task :set_branch do | |
| set :branch, prompt_branch_name | |
| end | |
| before "deploy:update" , "deploy:set_branch" | |
| end | |
| def prompt_branch_name | |
| BranchedDeploy.new.prompt | |
| end |
OlderNewer