Skip to content

Instantly share code, notes, and snippets.

@willsza
Last active June 10, 2017 11:42
Show Gist options
  • Save willsza/23572f70702b371b2c1e42b67eb54e5e to your computer and use it in GitHub Desktop.
Save willsza/23572f70702b371b2c1e42b67eb54e5e to your computer and use it in GitHub Desktop.
Campo do form has many through não aparece (Utilizando a gem wicked)
.row.wrapper.border-bottom.white-bg.page-heading
.col-lg-12
h1 Ordem de Serviços
.wrapper.wrapper-content.animated.fadeInRight
.row
.col-lg-12
.ibox.float-e-margins
= simple_form_for @service_order, method: :put, url: wizard_path, html: { id: 'form' } do |f|
= f.input :cost, as: :hidden, input_html: { value: @service_order.invoice_sub_total }
.ibox-title
h3 Selecione selecione o(s) serviço(s):
.ibox-content
.form-inputs
= f.simple_fields_for :service_order_services do |ff|
= ff.association :service
.form-actions
= f.button :submit, 'Salvar e avançar'
.ibox.float-e-margins
.ibox-content
= link_to raw('<i class="fa fa-angle-double-left"></i> Voltar'), service_orders_path, class: 'btn btn-default'
= link_to 'Avançar', next_wizard_path, class: 'btn btn-primary'
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '5.0.1'
gem 'bootstrap-sass', '~> 3.3.7'
gem 'font-awesome-rails', '~> 4.7', '>= 4.7.0.1'
gem 'jquery-rails', '~> 4.2', '>= 4.2.2'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0', '>= 5.0.6'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '~> 3.0', '>= 3.0.4'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.2', '>= 4.2.1'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks', '~> 5.0', '>= 5.0.1'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.6', '>= 2.6.1'
gem 'simple_form', '~> 3.4', github: 'plataformatec/simple_form', branch: 'master'
gem 'slim-rails', '~> 3.1', '>= 3.1.1'
gem 'country_select', '~> 3.0'
gem 'rails-jquery-tokeninput', '~> 0.2.6'
gem 'cocoon', '~> 1.2', '>= 1.2.9'
gem 'devise', '~> 4.2'
gem 'pundit', '~> 1.1'
gem 'brdinheiro', '~> 3.3'
gem 'brdata', '~> 3.3'
gem 'validates_cpf_cnpj', '~> 0.2.0'
gem 'wicked', '~> 1.3'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.3', '>= 1.3.13'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.0.5'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :production do
gem 'pg', '~> 0.18.4'
gem 'rails_12factor', '~> 0.0.3'
gem 'newrelic_rpm', '~> 3.15', '>= 3.15.2.317'
end
ruby "2.4.0"
class Service < ApplicationRecord
has_many :service_order_services
has_many :service_orders, through: :service_order_services
end
class ServiceOrder < ApplicationRecord
include ActionView::Helpers::NumberHelper
has_many :service_order_services, inverse_of: :service_order
has_many :services, through: :service_order_services, :class_name => 'Service'
accepts_nested_attributes_for :services
accepts_nested_attributes_for :service_order_services
end
class ServiceOrderService < ApplicationRecord
belongs_to :service
belongs_to :service_order
accepts_nested_attributes_for :service
end
class ServiceOrdersController < ApplicationController
before_action :set_service_order, only: [:show, :edit, :update, :destroy, :invoice_print]
before_action :authenticate_user!
# GET /service_orders
# GET /service_orders.json
def index
@service_orders = ServiceOrder.all
end
# GET /service_orders/1
# GET /service_orders/1.json
def show
end
def invoice_print
render :layout => "empty"
end
# GET /service_orders/new
def new
@service_order = ServiceOrder.new
@service_order.build_production
@service_order.service_order_services.build
end
# GET /service_orders/1/edit
def edit
end
# POST /service_orders
# POST /service_orders.json
def create
@service_order = ServiceOrder.new
@service_order.save(validate: false)
redirect_to service_order_step_path(@service_order, ServiceOrder.form_steps.first)
end
# PATCH/PUT /service_orders/1
# PATCH/PUT /service_orders/1.json
def update
respond_to do |format|
if @service_order.update(service_order_params)
format.html { redirect_to service_orders_url, notice: 'Service order was successfully updated.' }
format.json { render :show, status: :ok, location: @service_order }
else
format.html { render :edit }
format.json { render json: @service_order.errors, status: :unprocessable_entity }
end
end
end
# DELETE /service_orders/1
# DELETE /service_orders/1.json
def destroy
@service_order.destroy
respond_to do |format|
format.html { redirect_to service_orders_url, notice: 'Ordem de serviço excluída com sucesso.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_service_order
@service_order = ServiceOrder.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def service_order_params
params.require(:service_order).permit(
:description, :comments, :discount, :cost, :status, :client_id,
production_attributes: [ :id, :date_end ],
service_order_product_services_attributes: [ :id, :_destroy, :product_service_id, product_service_attributes: [ :id, :_destroy, :serial_number ]],
service_order_services_attributes: [ :id, :_destroy, :service_id, service_attributes: [ :id, :_destroy ]],
service_order_products_attributes: [ :id, :_destroy, :product_id, product_attributes: [ :id, :_destroy ]],
product_services_attributes: [ :id, :_destroy, :name, :serial_number]
)
end
end
class ServiceOrder::StepsController < ApplicationController
include Wicked::Wizard
steps *ServiceOrder.form_steps
def show
@service_order = ServiceOrder.find(params[:service_order_id])
@service_order.service_order_services.build
@service_order.build_production unless @service_order.production.present?
render_wizard
end
def update
@service_order = ServiceOrder.find(params[:service_order_id])
@service_order.update(service_order_params(wizard_value(step)))
render_wizard @service_order
end
def finish_wizard_path
@service_order
end
private
def service_order_params(step)
permitted_attributes = case wizard_value(step)
when :add_client
[ :client_id ]
when :add_product_services
[ service_order_product_services_attributes: [ :id, :_destroy, :product_service_id, product_service_attributes: [ :id, :_destroy, :serial_number ]]]
when :add_services
[ service_order_services_attributes: [ :id, :_destroy, :service_id, service_attributes: [ :id, :_destroy ]]]
when :add_products
[ service_order_products_attributes: [ :id, :_destroy, :product_id, :amount, product_attributes: [ :id, :_destroy ]]]
when :add_infos
[ :description, :comments, :discount, :cost, :status, production_attributes: [ :id, :date_end ]]
end
params.require(:service_order).permit(permitted_attributes).merge(form_step: wizard_value(step))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment