Skip to content

Instantly share code, notes, and snippets.

@iwada
Created September 6, 2011 17:44
Show Gist options
  • Save iwada/1198388 to your computer and use it in GitHub Desktop.
Save iwada/1198388 to your computer and use it in GitHub Desktop.
PinController
def fees
@title = "Pay Fees"
if request.post?
pin = Pin.check_pin_value(params[:pin][:number])
if pin.nil?
flash.now[:error] = "Unable to Pay Fees,Check You Pin and Make sure it has not been Used"
else
# pin_value = pin.pin_value
pin.update_attribute(:status,0)
flash.now[:success] ="Fees Paid"
end
end
end
pins/fees.html.erb
.....
Please Enter Your Pin :
<%= f.text_field :number %>
<%= f.submit "Pay Fees" , :class => "new" %>
....
Pin's Migration
......
t.integer :number
t.integer :value
t.boolean :status,:default => true...
class Pin < ActiveRecord::Base
attr_accessible :number ,:value,:pin_quantity,:serial_no,:user_id
attr_accessor :pin_quantity
before_save :create_pin
belongs_to :user
pin_value_regex = /^(12500|28500)$/
pin_quantity_regex = /^(12500|28500)$/
validates :pin_quantity, :presence => true,
:length => {:minimum => 2},
:numericality => true
validates :value , :presence=> true,
:format => {:with => pin_value_regex,
:message => "is not a valid value"}
def self.search(search, page) # this basically adds a search feature to Our page
paginate :per_page =>36, :page => page,
:conditions=> ['number like ? ' , "%#{search}%"],
:order => 'status ASC'
end
# '[[:<:]]#{search}[[:>:]]'
def create_pin
self.number = Time.now + rand(10_000_000-1_000_000)+1_000_000
end
class << self
def check_pin(pin_to_check) #Check the validity of a Pin, if it's been used or not, Remove self whe
#ready to fly
pin = find_by_number(pin_to_check)
if pin.nil? and pin.pin_status?
nil
else
pin
end
end
def check_pin_value(pin_to_check) #Checks and returns the value of a pin
pin = find_by_number(pin_to_check)
if pin.nil?
nil
else
return pin
#pin =Pin.select(:pin_value).where(:number => pin_to_check)
end
end
end
# Pin Encryption Methods
private
def encrypt_pin
self.salt = make_salt if new_record?
self.encrypted_pin = encrypt(pin)
end
def encrypt(string)
secure_hash("#{salt}--#{string}")
end
def make_salt
secure_hash("#{Time.now.utc}--#{pin}")
end
def secure_hash(string)
Digest::SHA2.hexdigest(string)
end
end
Each time the Form is submitted, the boolean attibute of status changes, but same with the value attribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment