Skip to content

Instantly share code, notes, and snippets.

@mpron
Last active June 11, 2019 23:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpron/8725840 to your computer and use it in GitHub Desktop.
Save mpron/8725840 to your computer and use it in GitHub Desktop.
#This is Rails 4.0.2 so the attr_accessible functionality is handled in the controller
class Bottle < ActiveRecord::Base
belongs_to :wine
belongs_to :user
belongs_to :location
end
#This is how data is being scraped in to my models.
require 'csv'
class CellarTracker
def self.import(username, password, user)
url = "https://www.cellartracker.com/xlquery.asp"
#"https://www.cellartracker.com/xlquery.asp?table=List&Location=1&User=#{username}&Password=#{password}"
result = HTTParty.get(url, :query => {
"table" => "Inventory",
"User" => username,
"Password" => password,
"Format" => "csv"
})
csv = CSV.parse(result, :headers => true)
wines = []
csv.each do |raw_row|
row = raw_row.to_hash
winery_name = row["Producer"].encode('utf-8')
vintage = row["Vintage"].encode('utf-8')
wine_name = row["Wine"].encode('utf-8')
bin = row["Location"]+" "+row["Bin"].encode('utf-8')
quantity = row["Quantity"].to_i
winery = Winery.where(:name => winery_name).first_or_create
wine = Wine.where(:name => wine_name, :winery_id => winery.id, :vintage => vintage).first_or_create
location = Location.where(:name => bin, :user_id => user.id).first_or_create
quantity.times do
Bottle.create!(:user_id => user.id, :wine_id => wine.id, :location_id => location.id)
end
wines << wine
end
wines
end
end
class Location < ActiveRecord::Base
has_many :bottles
has_many :wines, through: :bottles
end
%h3
= "#{@user.email}'s Cellar"
%p
%h4 Add Wine:
= form_for @user.bottles.new do |f|
= f.hidden_field :user_id, :value => @user.id
.form-group.form-inline
= f.select :wine_id, Wine.all.collect { |w| ["#{w.winery.name} #{w.vintage} #{w.name}", w.id] }, {}, {:class => "form-control"}
= f.submit 'Add Wine', :class => "btn btn-primary"
- if @bottles.count > 0
You have
= @bottles.count
bottles
%table.table
%tr
%th Location
%th Vintage
%th Name
%th Winery
%th Quantity
%th Actions
- @user_wines.each do |wine|
- wine.locations.each do |loc|
%tr
%td= loc.name
%td= wine.vintage
%td= wine.name
%td= wine.winery.name
%td= loc.bottles.where(:wine_id => wine.id).count
%td= link_to "Drink Bottle", wine_path(wine), :class => "btn btn-success btn-sm"
- else
%p You have no wines in your collection. Get to the store!
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :wines, through: :bottles
has_many :bottles
end
class UsersController < ApplicationController
before_action :set_user, :only => [:show, :add_wine]
def index
@users = User.all
end
def show
#@user.wines.order("name ASC").pluck(:name).uniq!
@user_wines = @user.wines.group("name, vintage")
@bottles = @user.bottles
end
def add_wine
raise params.inspect
end
private
def set_user
@user = User.find(params[:id])
end
end
class Wine < ActiveRecord::Base
belongs_to :winery
has_many :bottles
has_many :locations, through: :bottles
end
class Winery < ActiveRecord::Base
has_many :wines
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment