Class: Widgets::ManageProfile::Base

Inherits:
ErpApp::Widgets::Base
  • Object
show all
Defined in:
app/widgets/manage_profile/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.base_layoutObject



267
268
269
270
271
272
273
274
# File 'app/widgets/manage_profile/base.rb', line 267

def base_layout
  begin
    file = File.join(File.dirname(__FILE__), "/views/layouts/base.html.erb")
    IO.read(file)
  rescue
    return nil
  end
end

.titleObject



259
260
261
# File 'app/widgets/manage_profile/base.rb', line 259

def title
  "Manage Profile"
end

.widget_nameObject



263
264
265
# File 'app/widgets/manage_profile/base.rb', line 263

def widget_name
  File.basename(File.dirname(__FILE__))
end

Instance Method Details

#add_addressObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'app/widgets/manage_profile/base.rb', line 196

def add_address
  ActiveRecord::Base.connection.transaction do
    begin
      address_line_1 = params[:address_line_1].strip
      address_line_2 = params[:address_line_2].strip
      city = params[:city].strip
      geo_zone_id = params[:state].strip
      postal_code = params[:postal_code].strip
      contact_purpose = params[:contact_purpose]

      postal_address = current_user.party.add_contact(PostalAddress,
                                                      {
                                                          address_line_1: address_line_1,
                                                          address_line_2: address_line_2,
                                                          city: city,
                                                          geo_zone_id: geo_zone_id,
                                                          state: GeoZone.find(geo_zone_id).zone_name,
                                                          zip: postal_code,
                                                      },
                                                      ContactPurpose.find_by_internal_identifier(contact_purpose))

      {:json => {success: true,
                 message: 'Address added',
                 address: postal_address.to_hash(:only => [:id, :address_line_1, :address_line_2,
                                                           :city, :state, :zip],
                                                 :contact_purpose => ContactPurpose.find_by_internal_identifier(contact_purpose).description)}}
    rescue => ex
      Rails.logger.error ex.message
      Rails.logger.error ex.backtrace.join("\n")

      ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

      {:json => {success: false, message: 'Could not add address'}}
    end
  end
end

#add_email_addressObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/widgets/manage_profile/base.rb', line 112

def add_email_address
  ActiveRecord::Base.connection.transaction do
    begin
      email_address = params[:email_address].strip
      contact_purpose = params[:contact_purpose]

      email = current_user.party.add_contact(EmailAddress,
                                             {email_address: email_address},
                                             ContactPurpose.find_by_internal_identifier(contact_purpose))

      {:json => {success: true, message: 'Email added', email: email.to_hash(:only => [:id, :email_address],
                                                                             :contact_purpose => ContactPurpose.find_by_internal_identifier(contact_purpose).description)}}
    rescue => ex
      Rails.logger.error ex.message
      Rails.logger.error ex.backtrace.join("\n")

      ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

      {:json => {success: false, message: 'Could not add email'}}
    end
  end
end

#add_phone_numberObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/widgets/manage_profile/base.rb', line 154

def add_phone_number
  ActiveRecord::Base.connection.transaction do
    begin
      phone_number = params[:phone_number].strip
      contact_purpose = params[:contact_purpose]

      phone_number = current_user.party.add_contact(PhoneNumber,
                                                    {phone_number: phone_number},
                                                    ContactPurpose.find_by_internal_identifier(contact_purpose))

      {:json => {success: true, message: 'Phone number added', phone: phone_number.to_hash(:only => [:id, :phone_number],
                                                                                           :contact_purpose => ContactPurpose.find_by_internal_identifier(contact_purpose).description)}}
    rescue => ex
      Rails.logger.error ex.message
      Rails.logger.error ex.backtrace.join("\n")

      ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

      {:json => {success: false, message: 'Could not add phone number'}}
    end
  end
end

#indexObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/widgets/manage_profile/base.rb', line 5

def index
  @user = User.find(current_user)
  @individual = @user.party.business_party
  @email_addresses = @user.party.find_all_contacts_by_contact_mechanism(EmailAddress)
  @phone_numbers = @user.party.find_all_contacts_by_contact_mechanism(PhoneNumber)
  @postal_addresses = @user.party.find_all_contacts_by_contact_mechanism(PostalAddress)

  contact_purposes = ContactPurpose.all
  @purpose_hash={}
  contact_purposes.each do |p|
    @purpose_hash[p.description]=p.internal_identifier
  end

  countries= GeoCountry.all
  @countries_id=[]
  @countries_id << ["Country", "default"]
  countries.each do |c|
    @countries_id << [c.name, c.id]
  end

  states= GeoZone.all
  @states_id=[]
  @states_id << ["State", "default"]
  states.each do |s|
    @states_id << [s.zone_name, s.id]
  end

  render
end

#locateObject

should not be modified modify at your own risk



254
255
256
# File 'app/widgets/manage_profile/base.rb', line 254

def locate
  File.dirname(__FILE__)
end

#remove_addressObject



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'app/widgets/manage_profile/base.rb', line 233

def remove_address
  ActiveRecord::Base.connection.transaction do
    begin
      address_id = params[:address_id].strip

      PostalAddress.find(address_id).destroy

      {:json => {success: true, message: 'address removed'}}
    rescue => ex
      Rails.logger.error ex.message
      Rails.logger.error ex.backtrace.join("\n")

      ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

      {:json => {success: false, message: 'Could not remove address'}}
    end
  end
end

#remove_email_addressObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/widgets/manage_profile/base.rb', line 135

def remove_email_address
  ActiveRecord::Base.connection.transaction do
    begin
      email_address_id = params[:email_address_id].strip

      EmailAddress.find(email_address_id).destroy

      {:json => {success: true, message: 'Email removed'}}
    rescue => ex
      Rails.logger.error ex.message
      Rails.logger.error ex.backtrace.join("\n")

      ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

      {:json => {success: false, message: 'Could not remove email'}}
    end
  end
end

#remove_phone_numberObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/widgets/manage_profile/base.rb', line 177

def remove_phone_number
  ActiveRecord::Base.connection.transaction do
    begin
      phone_number_id = params[:phone_number_id].strip

      PhoneNumber.find(phone_number_id).destroy

      {:json => {success: true, message: 'Phone number removed'}}
    rescue => ex
      Rails.logger.error ex.message
      Rails.logger.error ex.backtrace.join("\n")

      ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

      {:json => {success: false, message: 'Could not remove phone number'}}
    end
  end
end

#update_passwordObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/widgets/manage_profile/base.rb', line 66

def update_password
  ActiveRecord::Base.connection.transaction do
    begin
      if @user = User.authenticate(current_user.username, params[:old_password])

        if !params[:new_password].blank? && !params[:password_confirmation].blank? && params[:new_password] == params[:password_confirmation]

          @user.password_confirmation= params[:password_confirmation]

          if @user.change_password!(params[:new_password])
            @message = "Password Updated"

            render :update => {:id => "#{@uuid}_result", :view => :success}
          else
            @message = "Error Updating Password"

            #### validation failed ####
            render :update => {:id => "#{@uuid}_result", :view => :error}
          end

        else
          #### password and password confirmation cant be blank or unequal ####
          @message = "Password Confirmation Must Match Password"

          render :update => {:id => "#{@uuid}_result", :view => :error}
        end
      else
        #### old password wrong ####
        @message = "Invalid Old Password"

        render :update => {:id => "#{@uuid}_result", :view => :error}
      end
    rescue => ex
      Rails.logger.error ex.message
      Rails.logger.error ex.backtrace.join("\n")

      ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

      @message = 'Could not update password'

      render :update => {:id => "#{@uuid}_result", :view => :error}
    end
  end

end

#update_user_informationObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/widgets/manage_profile/base.rb', line 35

def update_user_information
  #### Get appropriate models ####

  @user = User.find(current_user)
  @individual = @user.party.business_party

  @individual.current_first_name = params[:first_name]
  @individual.current_last_name = params[:last_name]
  @individual.current_middle_name = params[:middle_name]
  @user.email = params[:email]

  #### check validation then save and render message ####
  if @user.changed? || @individual.changed?
    if @user.valid? && @individual.valid?
      @user.save
      @individual.save
      @message = "User Information Updated"

      render :update => {:id => "#{@uuid}_result", :view => :success}
    else
      @message_cls = 'sexyerror'
      @message = "Error Updating User Information"
      render :update => {:id => "#{@uuid}_result", :view => :error}
    end
  else
    @message = "User Information Updated"
    render :update => {:id => "#{@uuid}_result", :view => :success}
  end

end