Skip to content

Instantly share code, notes, and snippets.

@silviupanaite
Last active August 17, 2018 10:41
Show Gist options
  • Save silviupanaite/4b0b9ec7080013364dd0c647de2ab1cb to your computer and use it in GitHub Desktop.
Save silviupanaite/4b0b9ec7080013364dd0c647de2ab1cb to your computer and use it in GitHub Desktop.
Prices
#app/models/product.rb
class Product < ActiveRecord::Base
belongs_to :category
has_many :prices, dependent: :destroy
has_many :promotion_products
has_many :promotions, through: :promotion_products
attr_accessible :name, :details, :category_id, :picture, :picture_cache, :prices_attributes
validates :name, :details, :category, presence: {message: "trebuie completat"}
validates :prices, presence: true, associated: true
accepts_nested_attributes_for :prices, allow_destroy: true
scope :active, -> { joins(:category).where("categories.active IS TRUE AND products.active IS TRUE") }
mount_uploader :picture, PictureUploader
include Authority::Abilities
self.authorizer_name = "RestaurantEntityAuthorizer"
RESPOND_OPTIONS = {
except: [:picture],
methods: [:picture_url],
include: {
prices: {
except: [:product_id, :cents], methods: [:price]
}
}
}
def restaurant
category.restaurant
end
def picture_url
"#{picture.url}"
end
end
#app/models/price.rb
class Price < ActiveRecord::Base
belongs_to :product
attr_accessible :price, :name, :cents, :quantity, :size
validates :price, presence: true, numericality: {greater_than: 0}
validates :name, presence: true
def price
cents / 100.00 if cents
end
def price=(value)
self.cents = value.to_d * 100
end
end
app/models/price.rb
class Price < ActiveRecord::Base
belongs_to :product
attr_accessible :price, :name, :cents, :quantity, :size
validates :price, presence: true, numericality: {greater_than: 0}
validates :name, presence: true
def price
cents / 100.00 if cents
end
def price=(value)
self.cents = value.to_d * 100
end
end
# Asa-mi apare cand dau cu product find
##<Product id: 2, name: "Orez Bob Mare Atifco 1kg", details: "eqweq", picture: nil, category_id: 1, active: true, rank: 0>
#lib/tasks/diana.rake
# aici sunt mai multe incercari clar nu e bine aici.
# ce ar trebui sa fac e sa caut dintr-un csv dupa nume, si sa fac update la pret articolului. am incercat cu product.update_attributes, doar ca nu am reusit sa construiesc ce are nevoie
# in ambele instante Price.where(product_id: product['id']).first! si Restaurant.where(id: 1).first.products.includes(:category) nu imi ia prices, am incercat si cu includes(:prices) si tot nu a mers
namespace :tasks do
require 'csv'
desc "update diana supermaket prices"
task :update_prices => :environment do |t, arg|
diana_files =['/tmp/diana.csv']
rest = Restaurant.where(id: 1).first.products.includes(:category)
diana_files.each do |file|
CSV.foreach(file,:headers => true) do |row|
name = row.to_hash['den_food']
price = row.to_hash['pret_nou']
rest.each do |product|
if product['name'] == name
prod = Price.where(product_id: product['id']).first!
pp prod
pp name + " => " + price
price = price * 100
pp price
if prod.update_attributes(price)
pp "Everything was good #{product.inspect}"
else
pp "Something went wrong #{row.to_hash}"
end
end
end
#User.create(id: id,gender: gender,age: age)
#{"name"=>"Orez Bob Mare Atifco 1kg", "details"=>"Orez Bob Mare Atifco 1kg", "category_id"=>"1", "picture_cache"=>"", "prices_attributes"=>{"0"=>{"name"=>"Orez Bob Mare Atifco 1kg", "price"=>"5.0", "quantity"=>"", "size"=>"", "_destroy"=>"false", "id"=>"4"}}}
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment