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
| gem 'paranoia' |
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 Post < ActiveRecord::Base | |
| has_many :comments | |
| default_scope { where(public: true) } | |
| end | |
| class Comment < ActiveRecord::Base | |
| belongs_to :post | |
| default_scope { where(deleted_at: nil) } | |
| def self.with_deleted |
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 Person | |
| def greet(name) | |
| "Hello, #{name}!" | |
| end | |
| end | |
| Person.instance_method(:greet).bind(Object.new)["World"] | |
| #=> TypeError: bind argument must be an instance of Person | |
| module Greeter | |
| def greet(name) |
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
| values = [1, 2, 3, 4, 5] | |
| values.inject(&:+) #=> 15 | |
| values.inject(10, &:+) #=> 25 | |
| values.map &1.method(:+) #=> [2, 3, 4, 5, 6] | |
| class String | |
| def translate(word) | |
| "#{word} => #{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
| class Article < ActiveRecord::Base | |
| def self.no_limit | |
| all.tap do |rel| | |
| logger.info rel.values #=> {:limit => 20} | |
| rel.limit = nil | |
| end | |
| end | |
| end | |
| class ArticlesController < ApplicationController |
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 Article < ActiveRecord::Base | |
| extend Pagination | |
| self.per_page = 20 | |
| end | |
| # Article.page(params[:page]) | |
| # Article.page(params[:page]).per_page(params[:per_page]) | |
| # Article.paginate(page: params[:page], per_page: params[:per_page]) |
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
| DB_FILE = 'tmp/test_db' | |
| FileUtils.mkdir_p File.dirname(DB_FILE) | |
| FileUtils.rm_f DB_FILE | |
| ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: DB_FILE | |
| ActiveRecord::Migration.class_exec do | |
| self.verbose = false | |
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 'rake/testtask' | |
| require 'bundler' | |
| Bundler::GemHelper.install_tasks | |
| tasks default: [:test] | |
| Rake::TestTask.new do |task| | |
| task.pattern = 'test/*_test.rb' | |
| 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
| dependent_associations = Article.reflect_on_all_associations.select do |association| | |
| association.options.has_key?(:dependent) | |
| 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
| require 'task/testtask' | |
| require 'rake/clean' | |
| task default: [:test] | |
| Rake::TestTask.new do |task| | |
| task.pattern = "test/*_test.rb" | |
| end | |
| CLEAN.include 'tmp' |