Advertisement
Guest User

Untitled

a guest
Aug 20th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.26 KB | None | 0 0
  1. # You are a user, I am a user
  2. class User < ActiveRecord::Base
  3.   extend Enumerize
  4.   extend FriendlyId
  5.   include Payment, ValidatorConcern
  6.  
  7.   friendly_id :slug_candidates, use: :slugged
  8.  
  9.   has_one :wallet, as: :owner
  10.   has_one :bank_account
  11.   accepts_nested_attributes_for :bank_account, reject_if: :all_blank, allow_destroy: true
  12.  
  13.   has_many :projects, inverse_of: :user
  14.   has_many :donations
  15.  
  16.   has_attached_file :avatar, styles: { medium: '300x300>', thumb: '100x100>' }, default_url: 'missing_:style.png'
  17.   validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
  18.   validates_uniqueness_of :email
  19.  
  20.   STATUS_LIST = [:personal, :business, :organization]
  21.  
  22.   devise :database_authenticatable, :registerable,
  23.          :recoverable, :rememberable, :trackable, :validatable, :omniauthable
  24.  
  25.   validate :country_alpha2, :birth_date, on: [:update, :create]
  26.  
  27.   before_save :capitalize
  28.   after_save :init_payment
  29.  
  30.   def self.from_oauth(auth, user_signed_in = nil)
  31.     user = user_signed_in
  32.     return user unless user.nil?
  33.  
  34.     oauth_create_or_get(auth)
  35.   end
  36.  
  37.   def self.oauth_create_or_get(auth)
  38.     user = User.first_or_create(email: auth.info.email) do |u|
  39.       set_attr_from_oauth(u, auth)
  40.     end
  41.     user.set_default_values
  42.     user
  43.   end
  44.  
  45.   def self.set_attr_from_oauth(user, auth)
  46.     info = auth.info
  47.     user.provider = auth.provider
  48.     user.uid = auth.uid
  49.     user.first_name = info.first_name
  50.     user.last_name = info.last_name
  51.     user.email = info.email
  52.     user.password = Devise.friendly_token[0, 20]
  53.   end
  54.  
  55.   def full_name
  56.     "#{first_name} #{last_name}".strip
  57.   end
  58.  
  59.   def set_default_values
  60.     self.first_name ||= I18n.t('anonymous')
  61.     self.email ||= "change@me-#{auth.uid}-#{auth.provider}.com"
  62.   end
  63.  
  64.   def full_address
  65.     "#{address}, #{postal_code}, #{Country.find_by_alpha2(country)[1]['name']}"
  66.   end
  67.  
  68.   private
  69.  
  70.   def capitalize
  71.     capitalizable = %w(first_name last_name)
  72.     attributes.each do |attr, val|
  73.       send("#{attr}=", val.to_s.strip.capitalize) if capitalizable.include?(attr)
  74.     end
  75.   end
  76.  
  77.   def init_payment
  78.     create_mango_user
  79.     create_mango_wallet
  80.   end
  81.  
  82.   def slug_candidates
  83.     [
  84.       :full_name,
  85.       [:full_name, :country],
  86.       [:full_name, :mango_user_id],
  87.     ]
  88.   end
  89.  
  90.   def self.permitted_params
  91.     [:id, :last_name, :first_name, :phone, :address, :postal_code, :country, :city, bank_account_attributes: BankAccount.permitted_params]
  92.   end
  93.  
  94.   enumerize :status, in: STATUS_LIST, default: :personal
  95. end
  96.  
  97. class Project < ActiveRecord::Base
  98.   extend FriendlyId
  99.   friendly_id :name, use: :slugged
  100.  
  101.   belongs_to :user, inverse_of: :projects
  102.   belongs_to :bank_account
  103.   belongs_to :category
  104.  
  105.   has_one :wallet, as: :owner
  106.  
  107.   has_many :donations
  108.   has_many :rewards
  109.   accepts_nested_attributes_for :rewards, reject_if: :all_blank, allow_destroy: true
  110.   accepts_nested_attributes_for :user
  111.  
  112.   validates_uniqueness_of :name
  113.  
  114.   def self.permitted_params
  115.     [:name, :description, :reasons, :challenge, :category_id, :start_date, :end_date, :target_amount, :video]
  116.   end
  117. end
  118.  
  119. class Reward < ActiveRecord::Base
  120.   has_many :donations
  121.   belongs_to :project
  122.  
  123.   def self.permitted_params
  124.     [:id, :description, :price, :name, :limit, :_destroy]
  125.   end
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement