Skip to content

Instantly share code, notes, and snippets.

Created November 2, 2013 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/7280146 to your computer and use it in GitHub Desktop.
Save anonymous/7280146 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
before_validation :iban_xor_rib
self.inheritance_column = nil
def rib?(rib)
bank, office, account, key = rib.match(/^(\d{5})(\d{5})([[:alnum:]]{11})(\d{2})$/).captures
account = account.each_char.inject('') do |sum, char|
char = char.to_i 36
sum += ((char + 2 ** ((char - 10)/9)).to_i % 10).to_s
end
result = bank + office + account + key
result.size >= 21 && result.to_i % 97 == 0
rescue
false
end
def iban_xor_rib
if type == 'iban'
validates_format_of :iban, allow_blank: false, with: /\A[A-Z]{2}/i, message: 'Country code is missing from the IBAN code'
end
if type == 'rib'
rib = bank + office + account + key
# rib = rib.delete(' ')
validates_format_of :account , allow_blank: false, with: rib?(rib), message: 'RIB is not valid'
end
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