Skip to content

Instantly share code, notes, and snippets.

View rubyrider's full-sized avatar
🎯
Focusing

Irfan Ahmed rubyrider

🎯
Focusing
View GitHub Profile
@rubyrider
rubyrider / event.rb
Last active August 29, 2015 14:06
Event Fetching Process
module FetchEvent
extend ActiveSupport::Concern
def fetch_additional_photo
@offset, limit, fixed = 50, 50, 50
excluding_pid_lists = [].join(',')
0.upto(@offset) do |i|
increment = (fixed*i)
@offset+=increment
##Candidate:
validates :user_id, presence: { error_code: 9001,
developer_message: I18n.t('validations.developer_message.required'),
more_info: I18n.t('name.required.more_info') }
validates :user_id, uniqueness: true, presence: { error_code: 9001,
developer_message: I18n.t('validations.developer_message.required'),
more_info: I18n.t('name.required.more_info') }
class Company < ActiveRecord::Base
validates :name ,presence: {error_code: 9001,
developer_message: I18n.t('validations.developer_message.required'),
more_info: I18n.t('name.required.more_info')
}
end
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]
# 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",
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
@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 }
@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 / 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 / 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