Skip to content

Instantly share code, notes, and snippets.

@yudapc
Last active August 30, 2017 14:45
Show Gist options
  • Save yudapc/fe0c31e8a453941345c56fb8ae760472 to your computer and use it in GitHub Desktop.
Save yudapc/fe0c31e8a453941345c56fb8ae760472 to your computer and use it in GitHub Desktop.
Rails console
---------------------------
Pengganti .find or .find_by
---------------------------
categories = outlet.categories
categories.select{|c| c[:id] == item[:category_id]}.first
---------------------------
Penganti check value hash
---------------------------
user = User.first
Old code:
if user[:username].present? && user[:password].present?
New Code:
user.values_at(:username, :password).all?(&:present?)
---------------------------
Get value from hash
---------------------------
a={"1"=>"adi","2"=>"amar","3"=>"rave","4"=>"sum"}
arr=["1","5","3"]
a.values_at(*arr).compact
#outlet ==> ["adi", "rave"]
cookies.permanent[:remember_token] = { value: access_token.token, domain: :all }
--------------------------------------
Query with filter child
--------------------------------------
@items.includes(:item_variants)
.where("item_variants.is_deleted IS FALSE")
.references(:item_variants).as_json(:include => :item_variants)
--------------------------------------
Using as_json
--------------------------------------
# GET /api/v1/items/custom_amount
def custom_amount
item = Item.active.find_by(outlet_id: current_single_outlet, name: "Custom Amount")
render json: item.as_json(:include => [
:category,
]).merge({"item_variants" => item.item_variants.active})
end
def show
only_po = [:id, :note, :created_at, :updated_at, :supplier_id, :order_no, :total, :status]
only_outlet = [:id, :name, :address, :phone_number, :city, :province, :postal_code]
only_supplier = [:id, :name, :phone, :email, :address, :city, :state, :postal_code]
only_creator = only_updater = [:id, :email, :first_name, :last_name]
only_history_detail = [:id, :beginning_stock, :quantity, :item_name, :price, :created_at, :is_item, :item_variant]
respond_to do |format|
format.json {
render json: @purchase_order.as_json(include: {
outlet: { only: only_outlet },
supplier: { only: only_supplier },
creator: { only: only_creator },
updater: { only: only_updater },
activities: { only: [:id, :key, :created_at] },
history_details: { include: { item_variant: {} }, only: only_history_detail }
}, only: only_po)
}
format.pdf do
render pdf: "Purchase Order ##{@purchase_order.order_no}",
margin: {
top: 0,
bottom: 0,
left: 0,
right: 0
}
end
end
end
$ rails c
##
##add column table:
##
ActiveRecord::Migration.add_column :table_name, :field_name, :field_type
##
##remove column table:
##
ActiveRecord::Migration.remove_column :table_name, :field_name
##
#manipulated nested attributes (item_variant) where is_deleted false
##
items = Item.limit(3)
items.includes(:item_variants).where("item_variants.is_deleted IS FALSE").references(:item_variants).as_json(include: :item_variants)
##
##Transactions
##
ActiveRecord::Base.transactions do
method1
method2
end
---------------------------------------------------
Sample query join with concat:
---------------------------------------------------
def self.search_business(page, search, start_date, end_date)
business_search_result = Business.joins(:outlets, :user)
if search.present?
business_search_result = business_search_result.where("businesses.name ILIKE ? OR
outlets.name ILIKE ? OR
users.first_name ILIKE ? OR
users.last_name ILIKE ? OR
concat(users.first_name, ' ', users.last_name) ILIKE ? OR
users.email ILIKE ?", "%#{search}%", "%#{search}%", "%#{search}%", "%#{search}%", "%#{search}%", "%#{search}%")
end
if start_date.present? && end_date.present?
business_search_result = business_search_result.where("outlets.end_date BETWEEN ? AND ?", start_date, end_date)
end
total_page = business_search_result.count / PER_PAGE
business_collections = business_search_result.page(page).per(PER_PAGE).map do |business|
billing_cycle = business.is_trial ? "Trial" : SubscriptionDiscount.find_by(month: business.current_plan)[:name]
if business.user
{
id: business.id,
name: business.name,
outlets: business.outlets.length,
owner_email: business.user.email,
owner_name: business.user.full_name,
expired_at: business.expired_at.strftime("%d %b %Y"),
billing_cycle: billing_cycle
}
end
end
businesses_filter = business_collections.uniq.nil? ? business_collections : business_collections.uniq
return {businesses: businesses_filter, total_page: total_page}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment