Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # You are a user, I am a user
- class User < ActiveRecord::Base
- extend Enumerize
- extend FriendlyId
- include Payment, ValidatorConcern
- friendly_id :slug_candidates, use: :slugged
- has_one :wallet, as: :owner
- has_one :bank_account
- accepts_nested_attributes_for :bank_account, reject_if: :all_blank, allow_destroy: true
- has_many :projects, inverse_of: :user
- has_many :donations
- has_attached_file :avatar, styles: { medium: '300x300>', thumb: '100x100>' }, default_url: 'missing_:style.png'
- validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
- validates_uniqueness_of :email
- STATUS_LIST = [:personal, :business, :organization]
- devise :database_authenticatable, :registerable,
- :recoverable, :rememberable, :trackable, :validatable, :omniauthable
- validate :country_alpha2, :birth_date, on: [:update, :create]
- before_save :capitalize
- after_save :init_payment
- def self.from_oauth(auth, user_signed_in = nil)
- user = user_signed_in
- return user unless user.nil?
- oauth_create_or_get(auth)
- end
- def self.oauth_create_or_get(auth)
- user = User.first_or_create(email: auth.info.email) do |u|
- set_attr_from_oauth(u, auth)
- end
- user.set_default_values
- user
- end
- def self.set_attr_from_oauth(user, auth)
- info = auth.info
- user.provider = auth.provider
- user.uid = auth.uid
- user.first_name = info.first_name
- user.last_name = info.last_name
- user.email = info.email
- user.password = Devise.friendly_token[0, 20]
- end
- def full_name
- "#{first_name} #{last_name}".strip
- end
- def set_default_values
- self.first_name ||= I18n.t('anonymous')
- self.email ||= "change@me-#{auth.uid}-#{auth.provider}.com"
- end
- def full_address
- "#{address}, #{postal_code}, #{Country.find_by_alpha2(country)[1]['name']}"
- end
- private
- def capitalize
- capitalizable = %w(first_name last_name)
- attributes.each do |attr, val|
- send("#{attr}=", val.to_s.strip.capitalize) if capitalizable.include?(attr)
- end
- end
- def init_payment
- create_mango_user
- create_mango_wallet
- end
- def slug_candidates
- [
- :full_name,
- [:full_name, :country],
- [:full_name, :mango_user_id],
- ]
- end
- def self.permitted_params
- [:id, :last_name, :first_name, :phone, :address, :postal_code, :country, :city, bank_account_attributes: BankAccount.permitted_params]
- end
- enumerize :status, in: STATUS_LIST, default: :personal
- end
- class Project < ActiveRecord::Base
- extend FriendlyId
- friendly_id :name, use: :slugged
- belongs_to :user, inverse_of: :projects
- belongs_to :bank_account
- belongs_to :category
- has_one :wallet, as: :owner
- has_many :donations
- has_many :rewards
- accepts_nested_attributes_for :rewards, reject_if: :all_blank, allow_destroy: true
- accepts_nested_attributes_for :user
- validates_uniqueness_of :name
- def self.permitted_params
- [:name, :description, :reasons, :challenge, :category_id, :start_date, :end_date, :target_amount, :video]
- end
- end
- class Reward < ActiveRecord::Base
- has_many :donations
- belongs_to :project
- def self.permitted_params
- [:id, :description, :price, :name, :limit, :_destroy]
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement