Class: Gluttonberg::Member
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Gluttonberg::Member
- Defined in:
- app/models/gluttonberg/member.rb
Instance Attribute Summary collapse
-
#image_delete ⇒ Object
Returns the value of attribute image_delete.
-
#return_url ⇒ Object
Returns the value of attribute return_url.
-
#term_and_conditions ⇒ Object
Returns the value of attribute term_and_conditions.
Class Method Summary collapse
- .contains_user?(user, list) ⇒ Boolean
- .does_email_verification_required ⇒ Object
- .enable_members ⇒ Object
-
.exportCSV ⇒ Object
export to a csv.
-
.find_column_position(csv_table, col_name) ⇒ Object
csv_table is two dimentional array col_name is a string.
- .generateRandomString(length = 10) ⇒ Object
-
.importCSV(file_path, invite, group_ids) ⇒ Object
takes complete path to csv file.
Instance Method Summary collapse
- #can_login? ⇒ Boolean
- #deliver_password_reset_instructions!(current_localization_slug = "") ⇒ Object
- #does_member_have_access_to_the_page?(page) ⇒ Boolean
- #full_name ⇒ Object
- #groups_name(join_str = ", ") ⇒ Object
- #have_group?(groups) ⇒ Boolean
Instance Attribute Details
#image_delete ⇒ Object
Returns the value of attribute image_delete.
15 16 17 |
# File 'app/models/gluttonberg/member.rb', line 15 def image_delete @image_delete end |
#return_url ⇒ Object
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_conditions ⇒ Object
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
209 210 211 212 213 214 |
# File 'app/models/gluttonberg/member.rb', line 209 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_required ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/models/gluttonberg/member.rb', line 56 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_members ⇒ Object
48 49 50 51 52 53 54 |
# File 'app/models/gluttonberg/member.rb', line 48 def self.enable_members if Rails.configuration.enable_members == true || Rails.configuration.enable_members.kind_of?(Hash) true else false end end |
.exportCSV ⇒ Object
export to a csv
233 234 235 236 237 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 |
# File 'app/models/gluttonberg/member.rb', line 233 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
220 221 222 223 224 225 226 227 228 229 |
# File 'app/models/gluttonberg/member.rb', line 220 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
70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/models/gluttonberg/member.rb', line 70 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”
101 102 103 104 105 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 |
# File 'app/models/gluttonberg/member.rb', line 101 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 user_info = { :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? || !user_info[key].kind_of?(String) user_info[key] = row[val] else user_info[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 user_info[:group_ids] = temp_group_ids end unless group_ids.blank? if user_info[:group_ids].blank? user_info[:group_ids] = group_ids else user_info[: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(user_info.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(user_info) 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
44 45 46 |
# File 'app/models/gluttonberg/member.rb', line 44 def can_login? !respond_to?(:can_login) || self.can_login == true end |
#deliver_password_reset_instructions!(current_localization_slug = "") ⇒ Object
31 32 33 34 |
# File 'app/models/gluttonberg/member.rb', line 31 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
82 83 84 |
# File 'app/models/gluttonberg/member.rb', line 82 def does_member_have_access_to_the_page?( page) self.have_group?(page.groups) end |
#full_name ⇒ Object
25 26 27 28 29 |
# File 'app/models/gluttonberg/member.rb', line 25 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
36 37 38 39 40 41 42 |
# File 'app/models/gluttonberg/member.rb', line 36 def groups_name(join_str=", ") unless groups.blank? groups.map{|g| g.name}.join(join_str) else "" end end |
#have_group?(groups) ⇒ Boolean
86 87 88 89 90 91 92 |
# File 'app/models/gluttonberg/member.rb', line 86 def have_group?(groups) if groups.find_all{|g| self.group_ids.include?(g.id) }.blank? false else true end end |