Last active
September 4, 2018 13:26
-
-
Save lazaronixon/ba50b6084050eb633a5a51a97d130f64 to your computer and use it in GitHub Desktop.
Simple Form Object
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
# name, state, city | |
class Address < ApplicationRecord | |
has_one :location, as: :locable, dependent: :destroy | |
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 AddressForm | |
include ActiveModel::Model | |
attr_accessor :address, :location | |
delegate :id, :persisted?, to: :address | |
delegate :name, :name=, :state, :state=, :city, :city=, to: :address | |
delegate :latitude, :latitude=, :longitude, :longitude=, to: :location | |
def save! | |
Address.transaction do | |
address.save! | |
location.save! | |
end | |
end | |
def update!(form_params) | |
assign_attributes(form_params) | |
save! | |
end | |
def self.model_name | |
Address.model_name | |
end | |
private | |
def address | |
@address ||= Address.new | |
end | |
def location | |
@location ||= address.build_location | |
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 AdressesController < ApplicationController | |
before_action :set_address, only: [:show, :edit, :update, :destroy] | |
def index | |
@search = Address.reverse_chronologically.ransack(params[:q]) | |
@adresses = paginate @search.result | |
respond_to do |format| | |
format.any :html, :json | |
format.csv { render_csv(@adresses) } | |
end | |
end | |
def show | |
fresh_when etag: @address | |
end | |
def new | |
@address_form = AddressForm.new | |
end | |
def edit | |
@address_form = AddressForm.new(address: @address, location: @address.location) | |
end | |
def create | |
@address_form = AddressForm.new(address_params) | |
@address_form.save! | |
respond_to do |format| | |
format.any(:html, :js) { redirect_to @address_form, notice: 'Endereço criado com sucesso.' } | |
format.json { render :show, status: :created } | |
end | |
end | |
def update | |
@address_form = AddressForm.new(address: @address, location: @address.location) | |
@address_form.update!(address_params) | |
respond_to do |format| | |
format.any(:html, :js) { redirect_to @address_form, notice: 'Endereço alterado com sucesso.' } | |
format.json { render :show } | |
end | |
end | |
def destroy | |
@address.destroy | |
respond_to do |format| | |
format.any(:html, :js) { redirect_to adresses_url, notice: 'Endereço apagado com sucesso.' } | |
format.json { head :no_content } | |
end | |
end | |
private | |
def set_address | |
@address = Address.find(params[:id]) | |
end | |
def address_params | |
params.require(:address).permit(:name, :state, :city, :latitude, :longitude) | |
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
# latitude, longitude | |
class Location < ApplicationRecord | |
belongs_to :locable, polymorphic: true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment