Skip to content

Instantly share code, notes, and snippets.

View rubyrider's full-sized avatar
🎯
Focusing

Irfan Ahmed rubyrider

🎯
Focusing
View GitHub Profile
def self.convert_key_to_human_readable_name(obj, options)
messages = []
class_name = obj.class.name.underscore
errors = obj.errors.messages
separator = options[:separator].present? ? options[:separator] : ','
errors.each do |key, value|
base_key = (options[key].present? && options.keys.include?(key)) ? options[key].to_s : key.to_s
values = value.each { |val| val.downcase }
values.flatten.join(" #{separator}") unless values.empty?
if key == :base
@rubyrider
rubyrider / deploy.rb
Created May 31, 2013 13:27
A very sample pattern of deployment file for capistrano
#For executing corn Jobs in server.
#set :whenever_command, "bundle exec whenever"
#require "whenever/capistrano"
require "delayed/recipes"
default_run_options[:pty] = true
set :ssh_options, {:forward_agent => true}
set :rails_env, "production"
# the day_statistics
include Mongoid::Document
field :date, type: Date
field :entity_id, type: Integer
field :todays_statistics, type: Hash
field :hotel_inventory, type: Hash
@rubyrider
rubyrider / After Refactoring
Last active December 21, 2015 20:39
Order and Purchase Code Refactoring
class OrderService
class << self
def find_or_create_purchase_for_order(order, credit_card_id, shipping_fee = 0.0)
buyer, seller, purchase, s_state = order.buyer, order.seller, Purchase.find_by_user_id_and_order_id(user.id, order.id), order.cart.shipping_address.state.upcase
sale_tax = SalesTax.find_by_user_id(vendor.id).send(s_state) rescue 0.0
total_chargeable_amount = calculate_total order.price, sale_tax, vendor.reppio_cut
create_purchase order, credit_card_id, shipping_fee, total_chargeable_amount, sales_tax # updated here
@rubyrider
rubyrider / alter_user_migration.rb
Last active December 22, 2015 10:59
Example of migration
class AlterUser < ActiveRecord::Migration
def up
rename_table :users, :customers
add_column :customers, :username, :string, limit: 25
change_column :customers, :email, :string, limit: 100
rename_column :customers, :password, :hashed_password
add_column :customers, :salt, :string, limit: 40
add_index :customers, :username
end
@rubyrider
rubyrider / gist:11131049
Created April 21, 2014 02:56
Adding permalink field to an existing database
# in migration
class AddPermalinkToPost < ActiveRecord::Migration
def change
add_column :posts, :permalink, :string
end
Post.find_each(&:save)
end
@rubyrider
rubyrider / find check digits
Created April 21, 2014 03:40
Luhn Algorithm
def number_with_check_digits(number)
digits = number.to_s.reverse.scan(/\d/).map { |x| x.to_i }
digits = digits.each_with_index.map { |d, i|
d *= 2 if i.even?
d > 9 ? d - 9 : d
}
sum = digits.inject(0) { |m, x| m + x }
mod = 10 - sum % 10
mod==10 ? 0 : mod
digits = number.to_s.reverse.scan(/\d/).map { |x| x.to_i }
Payment.transaction(isolation: :serializable) do # a postgresql feature for uniq transaction available on rails4 now
Payment.with(:line_item_id => line_item_id, :service_id => service_id) do |payment|
payment.process #
end
end
# Expected
# "error": {
# "status": 422,
# "code": "some_object/validation",
# "message": "Cannot create the {some_object} because provided data is not valid",
# "details": "More thorough description of a problem and probably even solution for curious users",
# "href": "http://some.url/to-describe/the-problem/in-even-more-details/optional",
# "errors": {
# "name": {
# "code": "validation/missing_required",
module ActiveModel
class Errors
attr_accessor :error_code, :developer_message, :more_info
def add(attribute, message = :invalid, options = {})
options.each { |k,v| instance_variable_set("@#{k}", v) }
message = normalize_message(attribute, message, options)
if exception = options[:strict]