Skip to content

Instantly share code, notes, and snippets.

@osulyanov
Last active September 19, 2019 07:57
Show Gist options
  • Save osulyanov/588f98e94ddbfb8e593deb7d4e71aed5 to your computer and use it in GitHub Desktop.
Save osulyanov/588f98e94ddbfb8e593deb7d4e71aed5 to your computer and use it in GitHub Desktop.
Controller for refactoring
# Service class emulates Rails ActiveRecord
class ActiveRecord
def initialize(params={}); end
end
# Order model
class Order < ActiveRecord
attr_accessor :country_code,
:first_name,
:last_name,
:gross_price,
:website
def valid?
!gross_price.nil?
end
def errors
[].tap{|e| e << {some_attr: 'Error message'}}
end
def self.last
all.last
end
def self.all
[
Order.new(country_code: 'RU',
first_name: 'Petr',
last_name: 'Sergeev',
gross_price: 10)
]
end
end
# Service class emulates Rails ApplicationController
class ApplicationController
def render(params)
puts params[:json].inspect
end
end
# Orders Controller
class OrdersController < ApplicationController
def create(params)
@gross_price = params[:gross_price]
@website = params[:website]
@country_code = params[:country_code] || previous_order.country_code
@first_name = params[:first_name] || previous_order.first_name
@last_name = params[:last_name] || previous_order.last_name
order = Order.new(checkout_params.merge(price_params))
order.website = @website
order.gross_price = price_params[:gross_price]
if order.valid?
render json: {order: order}
else
render json: {errors: order.errors}
end
end
def previous_order
Order.last
end
def checkout_params
return @checkout_params if @checkout_params
@checkout_params = {
first_name: @first_name,
last_name: @last_name,
country_code: @country_code,
}
end
def price_params
@price_params = @price_params || {
gross_price: @gross_price
}
end
end
# Samples
OrdersController.new.create(country_code: 'DE', first_name: 'Alex', gross_price: 10 )
OrdersController.new.create(country_code: 'DE', first_name: 'Alex' )
# frozen_string_literal: true
class Api::V1::Academy::OrdersController < Api::V1::Academy::BaseController
def create
@article_id = params[:article_id]
@user = params[:user]
@website = params[:website]
@gross_price = params[:gross_price]
@net_price_currency = params[:currency]
@charge_id = params[:charge_id]
@country_code = params[:country_code] || previous_order.try(:country_code) || user.try(:country_code)
@first_name = params[:first_name] || previous_order.try(:first_name) || user.try(:first_name)
@last_name = params[:last_name] || previous_order.try(:last_name) || user.try(:last_name)
@phone = params[:phone] || previous_order.try(:phone) || user.try(:phone)
@address_1 = params[:address_1] || previous_order.try(:address_1)
@address_2 = params[:address_2] || previous_order.try(:address_2)
@postal_code = params[:postal_code] || previous_order.try(:postal_code)
@city = params[:city] || previous_order.try(:city)
@subregion = params[:subregion] || previous_order.try(:subregion)
order = Ios::Order.new.order = build_order
if order.valid?
true
else
order.errors.messages.each { |attr, msg| errors.add(attr, msg.first) }
end
end
private
def permitted_params
params.permit(
:email, :first_name, :last_name, :address_1, :currency, :article_id,
:city, :postal_code, :country_code, :phone, :subregion, :gross_price, :charge_id
).merge(user: current_user, website: current_website)
end
def build_order
order = user ? user.orders.build(checkout_params.merge(price_params)) : ::Order.new(checkout_params.merge(price_params))
order.website = website
order.gross_price = price_params[:gross_price]
order.net_price_currency = price_params[:net_price_currency]
order.items.build do |item|
item.article = article
item.gross_price = price_params[:gross_price]
item.net_price_currency = price_params[:net_price_currency]
end
return order
end
def checkout_params
return @checkout_params if @checkout_params.present?
@checkout_params = {
first_name: first_name,
last_name: last_name,
phone: phone,
address_1: address_1,
address_2: address_2,
postal_code: postal_code,
city: city,
subregion: subregion,
country_code: country_code,
billing_integration: 'IOS',
charge_id: charge_id,
skip_validation: true
}
end
def price_params
@price_params = @price_params || {
gross_price: gross_price,
net_price_currency: net_price_currency
}
end
def previous_order
user&.orders&.last
end
def article
@article = if @article.present?
return @article
else
Plan.find(article_id)
end unless article_id.blank?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment