Last active
February 18, 2016 17:15
-
-
Save rvgarimrj/0199bd2de0c204033b0a to your computer and use it in GitHub Desktop.
Query para trazer as descricoes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Color < ActiveRecord::Base | |
has_many :variations | |
validates :cod, | |
:presence => true, | |
:uniqueness => true | |
validates :descricao, | |
:presence => true, | |
:uniqueness => true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Product < ActiveRecord::Base | |
has_many :variations | |
validates :cod, | |
:presence => true, | |
:uniqueness => true | |
validates :descricao, | |
:presence => true, | |
:uniqueness => true | |
validates :price,:numericality => true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Size < ActiveRecord::Base | |
has_many :variations | |
validates :descricao, | |
:presence => true, | |
:uniqueness => true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Variation < ActiveRecord::Base | |
belongs_to :product | |
belongs_to :size | |
belongs_to :color | |
before_save :gera_cod_barras | |
validates :product_id, | |
:presence => true | |
validates :size_id, | |
:presence => true | |
validates :color_id, | |
:presence => true | |
validates :quantity, | |
:presence => true, | |
:numericality => true | |
validates_uniqueness_of :product_id, scope: [:size_id, :color_id] | |
# def barcode(barcode) | |
# outputter = Barby::CairoOutputter.new(barcode) | |
# end | |
# include HasBarcode | |
# has_barcode :barcode, | |
# :outputter => :svg, | |
# :type => :code_39, | |
# :value => "1234567" | |
# Proc.new { |p| "#{p.barcode}" } | |
# def number | |
# self.barcode | |
# end | |
def gera_cod_barras | |
produto = self.product.cod | |
cor = self.color.cod | |
tamanho = self.size.descricao | |
cod_barras = produto + tamanho + cor | |
self.barcode = cod_barras | |
end | |
# def self.to_csv | |
# attributes = %w{barcode} | |
# CSV.generate do |csv| | |
# csv << attributes | |
# all.each do |variation| | |
# csv << variation.attributes.values_at(*attributes) | |
# end | |
# end | |
# end | |
def self.to_csv(options = {}) | |
CSV.generate(options) do |csv| | |
csv.add_row column_names | |
all.each do |variation| | |
values = variation.attributes.values | |
csv.add_row values | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VariationsController < ApplicationController | |
before_action :set_variation, only: [:show, :edit, :update, :destroy] | |
# require 'barby/outputter/cairo_outputter' | |
# def barcode_output( order ) | |
# barcode_string = order.barcode | |
# barcode = Barby::Code128B.new(barcode_string) | |
# # PNG OUTPUT | |
# data = barcode.to_image(height: 15, margin: 5).to_data_url | |
# end | |
# GET /variations | |
# GET /variations.json | |
def export | |
@data = Variation.order(:created_at) | |
# @data = Variation.select(:barcode).order(created_at: :desc) | |
respond_to do |format| | |
format.html { redirect_to root_url } | |
format.csv { send_data @data.to_csv } | |
end | |
end | |
def index | |
# @barcode = Barby::Code128B.new("160P10") rescue nil | |
# @outputter = Barby::HtmlOutputter.new(@barcode) | |
# @blob = Barby::PngOutputter.new(@barcode).to_png #Raw PNG data | |
# @barcode = Barby::HtmlOutputter.new('160P10') | |
# @barcode_for_html = Barby::HtmlOutputter.new(@barcode) | |
@variations = Variation.all | |
# @import = Variation::Import.new | |
respond_to do |format| | |
format.html | |
format.csv { send_data @variations} | |
end | |
# def import | |
# @import = Variation::Import.new variation_import_params | |
# if @import.save | |
# redirect_to variations_path, notice: "Importados #{@import.imported_count} Cod Barras" | |
# else | |
# @variations = Variation.all | |
# flash[:alert] = "Foram encontrados #{@import.errors.count} erros no CSV" | |
# render action: index | |
# end | |
# end | |
# require 'barby/outputter/png_outputter' | |
# blob = Barby::PngOutputter.new("12345").to_png #Raw PNG data | |
# File.open('barcode.png', 'wb'){|f| f.write blob } | |
#Convenience method | |
# File.open('barcode2.png', 'wb'){|f| f.write barcode.to_png } | |
# @barcode = Barby::EAN13.new('000000000') | |
# @barcode_for_html = Barby::CairoOutputter.new("0123456789") | |
# Variation.new.barcode # => Barby::Code39 object | |
# Variation.new.barcode_data # => <Barby::Code39 object>.to_png | |
end | |
# GET /variations/1 | |
# GET /variations/1.json | |
def show | |
end | |
# GET /variations/new | |
def new | |
@variation = Variation.new | |
@variacoes_js = Variation.joins(:color, :product, :size).includes(:color, :product, :size) | |
# # ActiveRecord::Base.include_root_in_json = true | |
gon.variacoes = @variacoes_js.to_json | |
# @tamanhos = Size.order(:descricao).select(:id,:descricao) | |
# gon.tamanhos = @tamanhos.to_json | |
# @produtos = Product.order(:cod).select(:cod,:descricao) | |
# gon.produtos = @produtos.to_json | |
end | |
# GET /variations/1/edit | |
def edit | |
end | |
# POST /variations | |
# POST /variations.json | |
def create | |
@variation = Variation.new(variation_params) | |
respond_to do |format| | |
if @variation.save | |
format.html { redirect_to variations_path, notice: 'Variation was successfully created.' } | |
format.json { render :index, status: :created, location: @variation } | |
else | |
format.html { render :new } | |
format.json { render json: @variation.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PATCH/PUT /variations/1 | |
# PATCH/PUT /variations/1.json | |
def update | |
respond_to do |format| | |
if @variation.update(variation_params) | |
format.html { redirect_to @variation, notice: 'Variation was successfully updated.' } | |
format.json { render :show, status: :ok, location: @variation } | |
else | |
format.html { render :edit } | |
format.json { render json: @variation.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /variations/1 | |
# DELETE /variations/1.json | |
def destroy | |
@variation.destroy | |
respond_to do |format| | |
format.html { redirect_to variations_url, notice: 'Variation was successfully destroyed.' } | |
format.json { head :no_content } | |
end | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_variation | |
@variation = Variation.find(params[:id]) | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def variation_params | |
params.require(:variation).permit(:product_id, :size_id, :color_id, :quantity, :barcode) | |
end | |
def variation_import_params | |
params.require(:variation_import).permit(:file) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Na action new eu preciso pegar o resultado do join entre os modelos com suas descricoes e o codigo de barras e passar isso pro js via json