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
28
29
30
# 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?
    
    validates_presence_of :name
    
    scope :with_email, lambda {|email| where(["email LIKE ?", "#{email}%"]) }
    scope :with_name, lambda {|name| where(["name LIKE ?", "#{name}%"]) }
    scope :with_role, lambda {|role_id| joins(:roles).merge(::Role.with_type(role_id)) }
    scope :defaults, with_role(::RoleType.default.id)
    scope :moderators, with_role(::RoleType.moderator.id)
    scope :admins, with_role(::RoleType.admin.id)            
  end
end

Instance Method Details

#to_csv(options = {}) ⇒ Object



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

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