Class: Gluttonberg::Member

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/gluttonberg/member.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#image_deleteObject

Returns the value of attribute image_delete.



14
15
16
# File 'app/models/gluttonberg/member.rb', line 14

def image_delete
  @image_delete
end

#return_urlObject

Returns the value of attribute return_url.



13
14
15
# File 'app/models/gluttonberg/member.rb', line 13

def return_url
  @return_url
end

#term_and_conditionsObject

Returns the value of attribute term_and_conditions.



13
14
15
# File 'app/models/gluttonberg/member.rb', line 13

def term_and_conditions
  @term_and_conditions
end

Class Method Details

.contains_user?(user, list) ⇒ Boolean

Returns:

  • (Boolean)


214
215
216
217
218
219
# File 'app/models/gluttonberg/member.rb', line 214

def self.contains_user?(user , list)
  list.each do |record|
    return true if record.id == user.id || record.email == user.email
  end
  false
end

.does_email_verification_requiredObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/gluttonberg/member.rb', line 61

def self.does_email_verification_required
  if Rails.configuration.enable_members == true
    true
  elsif Rails.configuration.enable_members.kind_of? Hash
    if Rails.configuration.enable_members.has_key?(:email_verification)
      Rails.configuration.enable_members[:email_verification]
    else
      true
    end
  else
    false
  end
end

.enable_membersObject



53
54
55
56
57
58
59
# File 'app/models/gluttonberg/member.rb', line 53

def self.enable_members
  if Rails.configuration.enable_members == true || Rails.configuration.enable_members.kind_of?(Hash)
    true
  else
    false
  end
end

.exportCSVObject

export to a csv



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'app/models/gluttonberg/member.rb', line 238

def self.exportCSV
  all_records = self.all
  csv_class = nil
  if RUBY_VERSION >= "1.9"
    require 'csv'
    csv_class = CSV
  else
    csv_class = FasterCSV
  end
  other_columns = {}
  csv_string = csv_class.generate do |csv|
      header_row = ["DATABASE ID",Rails.configuration.[:first_name],Rails.configuration.[:last_name], Rails.configuration.[:email], Rails.configuration.[:groups]]

      index = 0
      Rails.configuration..each do |key , val|
        if ![:first_name, :last_name , :email , :groups].include?(key)
          other_columns[key] = index + 5
          header_row << val
          index += 1
        end
      end
      csv << header_row

      all_records.each do |record|
        data_row = [record.id, record.first_name, record.last_name , record.email , record.groups_name("; ")]
        other_columns.each do |key , val|
          if !val.blank? && val >= 0
            data_row[val] = record.send(key)
          end
        end
        csv << data_row
      end
  end

  csv_string
end

.find_column_position(csv_table, col_name) ⇒ Object

csv_table is two dimentional array col_name is a string. if structure is proper and column name found it returns column index from 0 to n-1 otherwise nil



225
226
227
228
229
230
231
232
233
234
# File 'app/models/gluttonberg/member.rb', line 225

def self.find_column_position(csv_table  , col_name)
  if csv_table.instance_of?(Array) && csv_table.count > 0 && csv_table.first.count > 0
    csv_table.first.each_with_index do |table_col , index|
      return index if table_col.to_s.upcase == col_name.to_s.upcase
    end
    nil
  else
    nil
  end
end

.generateRandomString(length = 10) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/gluttonberg/member.rb', line 75

def self.generateRandomString(length=10)
  chars = ("A".."Z").to_a + ("0".."9").to_a
  numbers = ("0".."9").to_a
  similar_chars = %w{ i I 1 0 O o 5 S s }
  chars.delete_if {|x| similar_chars.include? x}
  numbers.delete_if {|x| similar_chars.include? x}
  newpass = ""
  1.upto(length-1) { |i| newpass << chars[rand(chars.size-1)] }
  1.upto(1) { |i| newpass << numbers[rand(numbers.size-1)] }
  newpass
end

.importCSV(file_path, invite, group_ids) ⇒ Object

takes complete path to csv file. and returns successfull_users , failed_users and updated_users arrays that contains user objects if user exist with given email then update its information otherwise create a new user for it returns [successfull_users , failed_users , updated_users , ] if csv format is incorrect then it will return a string “CSV file format is invalid”



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/models/gluttonberg/member.rb', line 106

def self.importCSV(file_path , invite , group_ids )
  begin
    if RUBY_VERSION >= "1.9"
      require 'csv'
      csv_table = CSV.read(file_path)
    else
      csv_table = FasterCSV.read(file_path)
    end
  rescue => e
    return "Please provide a valid CSV file with correct column names."
  end
  first_name_column_num =   self.find_column_position(csv_table , Rails.configuration.[:first_name] )
  last_name_column_num =   self.find_column_position(csv_table ,  Rails.configuration.[:last_name]  )
  email_column_num =   self.find_column_position(csv_table , Rails.configuration.[:email] )
  groups_column_num =   self.find_column_position(csv_table , Rails.configuration.[:groups] )
  other_columns = {}

  Rails.configuration..each do |key , val|
    if ![:first_name, :last_name , :email, :groups].include?(key)
      other_columns[key] = self.find_column_position(csv_table , val )
    end
  end

  successfull_users = []
  failed_users = []
  updated_users = []


  if first_name_column_num && last_name_column_num  && email_column_num
    csv_table.each_with_index do |row , index |
        if index > 0 # ignore first row because its meta data row
          #user information hash
           = {
            :first_name => row[first_name_column_num] ,
            :last_name => row[last_name_column_num] ,
            :email => row[email_column_num],
            :group_ids => []
          }
          other_columns.each do |key , val|
            if !val.blank? && val >= 0
              if row[val].blank? || ![key].kind_of?(String)
                [key] = row[val]
              else
                [key] = row[val].force_encoding("UTF-8")
              end
            end
          end

          #attach user to an industry if its valid
          unless groups_column_num.blank? || row[groups_column_num].blank?
            group_names = row[groups_column_num].split(";")
            temp_group_ids = []
            group_names.each do |group_name|
              group = Group.find(:first,:conditions=>{:name => group_name.strip})
              temp_group_ids << group.id unless group.blank?
            end
            [:group_ids] = temp_group_ids
          end

          unless group_ids.blank?
            if [:group_ids].blank?
              [:group_ids] = group_ids
            else
              [:group_ids] << group_ids
            end
          end

          user = self.find(:first , :conditions => { :email => row[email_column_num] } )
          if user.blank?
            # generate random password
            temp_password = self.generateRandomString
            password_hash = {
              :password => temp_password ,
              :password_confirmation => temp_password
            }

            # make user object
            user = self.new(.merge(password_hash))

            #if its valid then save it send an email and also add it to successfull_users array
            if user.valid?
              user.save
              if invite == "1"
                # we will regenerate password and send it member
                MemberNotifier.delay.welcome(user)
              end
              successfull_users << user
            else # if failed then add it to failed list
              failed_users << user
            end
          else
            if  !self.contains_user?(user , successfull_users) and !self.contains_user?(user , updated_users)
                if user.update_attributes()
                  updated_users << user
                else
                  failed_users << user
                end
            end
          end
        end # if csv row index > 0

    end #loop
  else
    return "Please provide a valid CSV file with correct column names"
  end #if
  [successfull_users , failed_users , updated_users ]
end

Instance Method Details

#can_login?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'app/models/gluttonberg/member.rb', line 49

def can_login?
  !respond_to?(:can_login) || self. == true
end

#deliver_password_reset_instructions!(current_localization_slug = "") ⇒ Object



36
37
38
39
# File 'app/models/gluttonberg/member.rb', line 36

def deliver_password_reset_instructions!(current_localization_slug = "")
  reset_perishable_token!
  MemberNotifier.password_reset_instructions(self.id,current_localization_slug).deliver
end

#does_member_have_access_to_the_page?(page) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/models/gluttonberg/member.rb', line 87

def does_member_have_access_to_the_page?( page)
  self.have_group?(page.groups)
end

#full_nameObject



30
31
32
33
34
# File 'app/models/gluttonberg/member.rb', line 30

def full_name
  self.first_name = "" if self.first_name.blank?
  self.last_name = "" if self.last_name.blank?
  self.first_name + " " + self.last_name
end

#groups_name(join_str = ", ") ⇒ Object



41
42
43
44
45
46
47
# File 'app/models/gluttonberg/member.rb', line 41

def groups_name(join_str=", ")
  unless groups.blank?
    groups.map{|g| g.name}.join(join_str)
  else
    ""
  end
end

#have_group?(groups) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
# File 'app/models/gluttonberg/member.rb', line 91

def have_group?(groups)
  if groups.find_all{|g| self.group_ids.include?(g.id)  }.blank?
    false
  else
    true
  end
end