Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'digest/sha1'
- class ImportStores < ActiveRecord::Migration
- class Store < ActiveRecord::Base
- has_one :address, :as => :addressable
- has_many :phones, :as => :owner
- has_many :users
- end
- class Address < ActiveRecord::Base
- belongs_to :addressable, :polymorphic => true
- end
- class Phone < ActiveRecord::Base
- belongs_to :owner, :polymorphic => true
- acts_as_list :scope => :owner_id
- def before_save
- number_before_type_cast.gsub!(/[^0-9]/, "") unless number.nil?
- end
- end
- class User < ActiveRecord::Base
- attr_accessor :password
- belongs_to :store
- acts_as_authorized_user
- before_save :encrypt_password
- # Encrypts some data with the salt.
- def self.encrypt(password, salt)
- Digest::SHA1.hexdigest("--#{salt}--#{password}--")
- end
- # Encrypts the password with the user salt
- def encrypt(password)
- self.class.encrypt(password, salt)
- end
- protected
- # before filter
- def encrypt_password
- return if password.blank?
- self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
- self.crypted_password = encrypt(password)
- end
- end
- def self.up
- require "faster_csv"
- FasterCSV.foreach 'db/stores.csv', :headers => true do |row|
- unless row[0].blank?
- store = Store.new
- store.number = row[0]
- store.email = row[9]
- store.save
- address = Address.new
- address.address1 = row[2]
- address.city = row[3]
- address.state = row[4]
- address.zipcode = row[5]
- address.addressable = store
- phone1 = Phone.new
- phone1.number = row[7]
- phone1.position = 0
- phone1.owner = store
- phone2 = Phone.new
- phone2.number = row[8]
- phone2.owner = store
- user = User.new
- user.login = store.number
- user.first_name = store.number
- user.last_name = row[1]
- user.password = "motors"
- user.store = store
- user.save
- address.save
- phone1.save
- phone2.save
- end
- end
- end
- def self.down
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement