Skip to content

Instantly share code, notes, and snippets.

@Bahanix
Forked from anonymous/_form.html.haml
Last active December 27, 2015 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bahanix/7280207 to your computer and use it in GitHub Desktop.
Save Bahanix/7280207 to your computer and use it in GitHub Desktop.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
= simple_form_for(@account) do |f|
= f.error_notification
= f.input :type, collection:['iban', 'rib'], include_blank: false
#iban
.inputs
= f.input :iban, :include_blank => false
= f.input :bic, :include_blank => false
= f.input :address
.action_views
= f.button :submit
#rib{ style: 'display: none;' }
.inputs
= f.input :bank, :include_blank => false
= f.input :office, :include_blank => false
= f.input :account, :include_blank => false
= f.input :key, :include_blank => false
= f.input :address
.action_views
= f.button :submit
:javascript
$("#account_type").change(function() {
var selection = $("#account_type").val();
if (selection == 'rib') {
$("#iban").hide();
$("#rib").show();
}
if (selection == 'iban') {
$("#rib").hide();
$("#iban").show();
}
});
class Account < ActiveRecord::Base
belongs_to :user
validate :iban_xor_rib
self.inheritance_column = nil
private
def iban_xor_rib
if type == 'rib'
errors.add type, :mandatory and return if [bank, office, account, key].any? &:blank?
rib = "#{bank}#{office}#{account}#{key}"
iban = "#{rib}FR00".each_char.inject('') do |sum, char|
sum += char.to_i(36).to_s
end.to_i
iban = "FR%.2d%s" % [98 - (iban % 97), rib]
end
errors.add type, :mandatory and return if iban.blank?
errors.add type, :country and return unless iban.to_s =~ /^[A-Z]{2}/i
tmp = iban.gsub(/[A-Z]/) { |p| (p.respond_to?(:ord) ? p.ord : p[0]) - 55 }
errors.add type, :format unless (tmp[6..tmp.length-1].to_s+tmp[0..5].to_s).to_i % 97 == 1
errors.add type, :country unless iban =~ /\A[A-Z]{2}/i
end
end
class AccountsController < ApplicationController
before_action :set_account, only: [:show, :edit, :update, :destroy]
# GET /accounts
# GET /accounts.json
def index
@accounts = current_user.accounts.all
end
# GET /accounts/1
# GET /accounts/1.json
def show
authorize! :read, @account
end
# GET /accounts/new
def new
@account = current_user.accounts.new
authorize! :create, @account
end
# GET /accounts/1/edit
def edit
authorize! :update, @account
end
# POST /accounts
# POST /accounts.json
def create
@account = current_user.accounts.new(account_params)
authorize! :create, @account
respond_to do |format|
if @account.save
format.html { redirect_to @account, notice: 'Account was successfully created.' }
format.json { render action: 'show', status: :created, location: @account }
else
format.html { render action: 'new' }
format.json { render json: @account.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /accounts/1
# PATCH/PUT /accounts/1.json
def update
authorize! :update, @account
respond_to do |format|
if @account.update(account_params)
format.html { redirect_to @account, notice: 'Account was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @account.errors, status: :unprocessable_entity }
end
end
end
# DELETE /accounts/1
# DELETE /accounts/1.json
def destroy
authorize! :destroy, @account
@account.destroy
respond_to do |format|
format.html { redirect_to accounts_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_account
@account = current_user.accounts.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def account_params
params.require(:account).permit(:type, :iban, :bic, :bank, :office, :account, :key, :address)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment