Skip to content

Instantly share code, notes, and snippets.

@chris-ramon
Last active January 4, 2016 03:39
Show Gist options
  • Save chris-ramon/8563629 to your computer and use it in GitHub Desktop.
Save chris-ramon/8563629 to your computer and use it in GitHub Desktop.
rails quick dev
# xcode 5.1 error when install thrift
bundle config build.thrift --with-cppflags='-D_FORTIFY_SOURCE=0'
# When debugging in ruby 2.1.0
# error: cannot find vm_core.h
# we have to downgrade to ruby 2.0.0
# to start using -> ruby-debug-ide
# Nokogiri and libxml
# Reason: Incompatible library version: nokogiri.bundle requires version 11.0.0 or later,
# but libxml2.2.dylib provides version 10.0.0
http://nokogiri.org/tutorials/installing_nokogiri.html
gem install nokogiri -v '1.6.1' -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2
# problems installing rmagick gem
C_INCLUDE_PATH=/usr/local/Cellar/imagemagick/6.8.9-1/include
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/
# then
gem install rmagick
# create skeleton rails app current dir
rails new .
# after gem "active_model_serializers"
# run
rails g serializer user
# mocking using mocha gem
sheets = mock()
sheets.stubs(:first).returns(1)
Roo::Base.any_instance.stubs(:sheets).returns(sheets)
# list available rake tasks
rake --tasks
# change locale
I18n.locale = 'en'
# searchkick
Product.reindex
# rake tasks, passing strings
rake seeds:organization['cool_org']
heroku run rake seeds:user[1,'3890@gmail.com','3890@gmail.com',45996130,'ttt','sss','',24]
# searchkick
class Item
def search_data
{user_ids: users.pluck(:id)}
end
end
Item.search "Foo", where: {user_ids: 1}
class Project < ActiveRecord::Base
def search_data
attributes.merge(
categories_title: categories.map(&:title)
)
end
end
search = Project.search 'something', facets: [:categories_title]
User.find(:all, limit: 10,
joins: "LEFT JOIN `user_points` ON user_points.user_id = users.id" ,
select: "users.*, count(user_points.id)",
group: "user_points.user_id")
# factory girl
FactoryGirl.create_list(:employee, 10, organization_id: 3)
# format tables in rails console
gem install 'hirb'
Hirb::View.enable
# rspec let, context
# describe for things, context for states
# describe '#matriculate' => thing
# context 'when the student is ....' => state
# befor for actions, let for instance variables
# loading external files to controllers
# app/services are loaded
# for custom
config.autoload_paths += Dir["#{Rails.root}/lib"]
# devise
# after gem 'devise'
rails generate devise:install
rails generate devise User
rake db:migrate
# we need to create this file in order to let rspec know about devise
spec/support/devise.rb
# then add this code to devise.rb
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
# custom name reference models
belongs_to :reply_to_user, class_name: "User"
# console
# create user via rails console
User.create!({email: 'rr@gmail.comm', password: '123456789', password_confirmation: '123456789'})
# table schema sqlite
PRAGMA table_info(users);
# clear tmp
rake tmp:clear
# undo last migration
rake db:rollback
# undo to specific migration
rake db:migrate:down version=847583457438957
# create tables and databases
rake db:setup
# migrations
rails generate migration CreateUploads
# polymorphic associations
t.references :commentable, polymorphic: true
# or
t.integer :commentable_id
t.string :commentable_type
# run dev migrations
rake db:migrate RAILS_ENV=development
# after installing 'rspec-rails' gem
rails generate rspec:install
# to run rspec tests
rspec
# run migrations for tests
rake db:migrate RAILS_ENV=test
# debugging
require 'debugger'; debugger
# set resource response json as default
resources :posts, defaults: {format: :json}
# run tests
rake test
# list all gems in current gemset
gem list
# ignore documentation, takes to long! and dash rocks ;)
gem install --no-document
# list all gemsets and current gemset will be highlighted
# set default rvm
rvm use ruby-2.1.0 --default
# rename gemset
rvm gemset rename old_name new_name
# delete gemset
rvm gemset delete gemset_name
# list all gemsets
rvm gemset list
# list all installed rubies
rvm list
# list all rubies that could be installed
rvm list known
# create and set the ruby version you will use, also creates the hidden files that set it.
rvm use ruby-2.0.0-p353@rails_angularjs_demo --create --ruby-version
# if latest version of ruby is not listed
rvm get stable
#
rails generate scaffold Post title:string body:string
rake db:migrate
# ignoring files
.DS_Store
.idea/
rails_angularjs_demo.iml
.ruby-version
.ruby-gemset
# ParameterMissing, validation errors.
rescue_from(ActionController::ParameterMissing) do |parameter_missing_exception|
error = {}
error[parameter_missing_exception.param] = ['parameter is required']
response = { errors: [error] }
render json: response, status: :unprocessable_entity
end
# enable cors
gem 'rack-cors'
config.middleware.insert_before Warden::Manager, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :put, :delete, :options]
end
end
# testing api with chrome rest client
{
"user":{
"email": "r@gmail.comm",
"password": "123456"
}
}
# testing custom sessions controller
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
end
# faster tests with spring
gem 'spring'
gem 'spring-commands-rspec'
bundle exec spring binstub --all
spring binstub --all
spring rspec
spring status
spring stop
# transactions
Employee.transaction do
risk_insurance = employee.risk_insurance
if risk_insurance.present? && params.require(:employee).has_key?(:risk_insurance)
unless risk_insurance.update(risk_insurance_params)
errors << {risk_insurance: risk_insurance.errors.full_messages}
raise ActiveRecord::Rollback.new
end
end
medical_exam = employee.medical_exam
if medical_exam.present? && params.require(:employee).has_key?(:medical_exam)
unless medical_exam.update(medical_exam_params)
errors << {medical_exam: medical_exam.errors.full_messages}
raise ActiveRecord::Rollback.new
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment