- Load rails app from irb:
require_relative 'config/environment.rb'(after thatRails.applicationis available) - Translation key for a model (ex. MyModel):
MyModel.model_name.i18n_key - Clean and recompile assets:
rake assets:clean assets:clobber assets:precompile(preperateRAILS_ENV=productionto set env) - Enable/disable cache in development:
rake dev:cache - Dev routes:
/rails/info/properties-/rails/info/routes - Search for notes comments (
[TODO],[FIXME], etc.):rake notes - Dump/restore DB structure:
rake db:structure:dump-rake db:structure:load - Funny method:
ActionDispatch::IntegrationTest.i_suck_and_my_tests_are_order_dependent!
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 'ffi' | |
| # Example 1: libc - puts | |
| module MyLib | |
| extend FFI::Library | |
| ffi_lib 'c' | |
| attach_function :puts, [ :string ], :int | |
| 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 'dry-validation' | |
| schema = Dry::Validation.Schema do | |
| required(:name).filled | |
| required(:age).maybe(:int?) | |
| optional(:sex).value( included_in?: %w(M F) ) | |
| end | |
| hash = { name: 'Jane', email: '[email protected]' } | |
| hash[:age] = 21 |
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
| # Set values in new block | |
| class MyClass | |
| attr_accessor :name, :val | |
| def initialize | |
| yield self if block_given? | |
| end | |
| end | |
| MyClass.new do |obj| |
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
| # Ruby offers different ways to extend classes / monkey patch / override methods. | |
| # A good way to execute code before or after a method of a class is using prepend | |
| class Test | |
| def fun | |
| puts 'Inside fun()' | |
| end | |
| end | |
| Test.new.fun |
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
| # Rails - sometimes it's needed to execute some code before anything else but after Rails core is loaded. A good place to achieve this is using after_initialize in config/application.rb: | |
| # ... config requires ... | |
| module MyApp | |
| class Application < Rails::Application | |
| config.after_initialize do | |
| # init code here... | |
| end | |
| 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
| # An implementation of static method chaining - like ActiveRecord, for example: Model.where( condition: true ) | |
| class Chain | |
| attr_accessor :ref, :val | |
| def initialize( ref, val = {} ) | |
| @ref = ref | |
| @val = val | |
| 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
| # Look for a specific string (optionally filtering files with a pattern) of my projects | |
| require 'git' | |
| require 'pathname' | |
| BASE_PATH='/projects' | |
| search='class_eval' | |
| filter_files='*.rb' # Empty for every file | |
| Pathname.new( BASE_PATH ).children.each do |path| |
- To ignore ActiveAdmin exceptions while migrating: update routes.rb:
ActiveAdmin.routes(self) rescue ActiveAdmin::DatabaseHitDuringLoad
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
| # All specs in the diff commits from develop (useful to check a specific branch): | |
| git diff-tree --no-commit-id --name-only -r develop..HEAD | grep '_spec\.rb' | sort | uniq > /tmp/specs | |
| # All specs in a specific range of commits: | |
| git diff-tree --no-commit-id --name-only -r HEAD~3..HEAD | grep '_spec\.rb' | sort | uniq > /tmp/specs | |
| # Execute them: | |
| while read p; do; if [ -f "$p" ]; then; be rspec "$p" || break; fi; done </tmp/specs |
OlderNewer