Module: Sunrise::Models::User::ClassMethods

Defined in:
lib/sunrise/models/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sunrise/models/user.rb', line 13

def self.extended(base)
  base.class_eval do        
    has_many :roles, :dependent => :delete_all, :autosave => true
    has_one :avatar, :as => :assetable, :dependent => :destroy, :autosave => true
    
    before_validation :generate_login, :if => :has_login?
    before_create :set_default_role, :if => :roles_empty?
    
    scope :with_email, lambda {|email| where(["email LIKE ?", "#{email}%"]) }
    scope :with_role, lambda {|role| joins(:roles).where(["`roles`.role_type = ?", role.id]) }
    scope :defaults, with_role(::RoleType.default)
    scope :moderators, with_role(::RoleType.moderator)
    scope :admins, with_role(::RoleType.admin)            
  end
end

Instance Method Details

#to_csv(options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sunrise/models/user.rb', line 29

def to_csv(options = {})
  options = { :columns => [:id, :email, :name, :current_sign_in_ip] }.merge(options)
  query = unscoped.order("#{quoted_table_name}.id ASC").select(options[:columns])

  FasterCSV.generate do |csv|
    csv << options[:columns]
    
    query.find_in_batches do |group|
      group.each do |user|
        csv << options[:columns].inject([]) do |items, attr_name|
          items << user.send(attr_name)
        end
      end
    end
  end
end