kaui 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/app/controllers/kaui/account_emails_controller.rb +54 -0
  2. data/app/controllers/kaui/accounts_controller.rb +6 -0
  3. data/app/controllers/kaui/tag_definitions_controller.rb +10 -10
  4. data/app/controllers/kaui/tags_controller.rb +85 -0
  5. data/app/helpers/kaui/killbill_helper.rb +54 -0
  6. data/app/models/kaui/account.rb +4 -2
  7. data/app/models/kaui/account_email.rb +48 -0
  8. data/app/views/kaui/account_emails/_account_emails_table.html.erb +10 -0
  9. data/app/views/kaui/account_emails/_form.html.erb +33 -0
  10. data/app/views/kaui/account_emails/edit.html.erb +6 -0
  11. data/app/views/kaui/account_emails/new.html.erb +3 -0
  12. data/app/views/kaui/account_emails/show.html.erb +24 -0
  13. data/app/views/kaui/accounts/show.html.erb +5 -1
  14. data/app/views/kaui/tag_definitions/index.html.erb +1 -1
  15. data/app/views/kaui/tags/_form.html.erb +25 -0
  16. data/app/views/kaui/tags/edit.html.erb +6 -0
  17. data/app/views/kaui/tags/index.html.erb +25 -0
  18. data/app/views/kaui/tags/new.html.erb +5 -0
  19. data/app/views/kaui/tags/show.html.erb +15 -0
  20. data/config/routes.rb +3 -0
  21. data/lib/kaui/version.rb +1 -1
  22. data/test/dummy/log/development.log +52294 -1360
  23. data/test/dummy/log/test.log +94034 -2851
  24. data/test/dummy/test/fixtures/bill_cycle_days.yml +3 -0
  25. data/test/dummy/tmp/cache/assets/C94/4E0/sprockets%2Fea1476dc10a3348303f74d111f70441a +0 -0
  26. data/test/dummy/tmp/cache/assets/{D0E/370/sprockets%2F27630db2080819f67c8a0ed5e491b7cb → CAA/680/sprockets%2F3824d037523f650518fb22acab75559d} +0 -0
  27. data/test/dummy/tmp/cache/assets/CD0/9D0/sprockets%2F5c9508c21501c73fbe00473a09b1f5f4 +0 -0
  28. data/test/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
  29. data/test/dummy/tmp/cache/assets/CDF/1F0/sprockets%2F76ac5628a0c4d1b976cb622ec4493751 +381 -0
  30. data/test/dummy/tmp/cache/assets/CEA/300/sprockets%2Fdf2ad5c9d0990441c2bf59883383d652 +0 -0
  31. data/test/dummy/tmp/cache/assets/{C8D/6D0/sprockets%2F0ce756f49d61795508c543a702955fc2 → CF7/710/sprockets%2F86d43448e1fc383cb6f3d752ef288882} +0 -0
  32. data/test/dummy/tmp/cache/assets/D17/DD0/sprockets%2F665455ecdc7609b23f4ecb366d86055a +9409 -0
  33. data/test/dummy/tmp/cache/assets/D32/200/sprockets%2Ffa467106e01bda5d6246baea72159d64 +0 -0
  34. data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  35. data/test/dummy/tmp/cache/assets/D39/5E0/sprockets%2F59fd338be48a81a17a2a785cbd1612b4 +0 -0
  36. data/test/dummy/tmp/cache/assets/D44/170/sprockets%2Fac15571bce3f926a498da7cd09322d97 +0 -0
  37. data/test/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
  38. data/test/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
  39. data/test/dummy/tmp/cache/assets/D6C/710/sprockets%2F1765773caead06c0a6a19ea9de2453f7 +0 -0
  40. data/test/dummy/tmp/cache/assets/D76/910/sprockets%2Ff99b4bdc434e11e8634e6af62fe805e0 +0 -0
  41. data/test/dummy/tmp/cache/assets/{D4A/DD0/sprockets%2F0f813582283b7f56fd83fe3d52be7e2c → D9F/160/sprockets%2F510462e1ebd5dbb7ae20888b77f9bed2} +0 -0
  42. data/test/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
  43. data/test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
  44. data/test/dummy/tmp/cache/assets/E07/1E0/sprockets%2F5a3fe6e98fdf72bbe75c605e54ebc5b0 +9409 -0
  45. data/test/dummy/tmp/cache/assets/E20/230/sprockets%2F7d3b1348fdf74cf1b6ba2107fbbac5af +0 -0
  46. data/test/dummy/tmp/cache/assets/{DF1/1A0/sprockets%2F7f6b7bfea83401d3106b7fadac81ea4b → E22/F40/sprockets%2Faffacf2e6be325520bd3bfd2096b6dd4} +0 -0
  47. data/test/dummy/tmp/cache/assets/E24/9D0/sprockets%2Fb966ab0a50fda3c156fbad4d8ab73ef6 +381 -0
  48. data/test/dummy/tmp/pids/server.pid +1 -0
  49. data/test/functional/kaui/account_emails_controller_test.rb +51 -0
  50. data/test/functional/kaui/tag_definitions_controller_test.rb +5 -5
  51. data/test/functional/kaui/tags_controller_test.rb +51 -0
  52. data/test/test_helper.rb +5 -1
  53. metadata +39 -10
@@ -0,0 +1,54 @@
1
+ module Kaui
2
+ class AccountEmailsController < ApplicationController
3
+ # GET /account_emails/1
4
+ # GET /account_emails/1.json
5
+ def show
6
+ @account_id = params[:id]
7
+ @account_emails = AccountEmail.where(:account_id => @account_id)
8
+
9
+ respond_to do |format|
10
+ format.html # show.html.erb
11
+ format.json { render :json => @account_email }
12
+ end
13
+ end
14
+
15
+ # GET /account_emails/new
16
+ # GET /account_emails/new.json
17
+ def new
18
+ @account_email = AccountEmail.new(:account_id => params[:account_id])
19
+
20
+ respond_to do |format|
21
+ format.html # new.html.erb
22
+ format.json { render :json => @account_email }
23
+ end
24
+ end
25
+
26
+ # POST /account_emails
27
+ # POST /account_emails.json
28
+ def create
29
+ @account_email = AccountEmail.new(params[:account_email])
30
+
31
+ respond_to do |format|
32
+ if @account_email.save
33
+ format.html { redirect_to account_email_path(@account_email), :notice => 'Account email was successfully created.' }
34
+ format.json { render :json => @account_email, :status => :created, :location => @account_email }
35
+ else
36
+ format.html { render :action => "new" }
37
+ format.json { render :json => @account_email.errors, :status => :unprocessable_entity }
38
+ end
39
+ end
40
+ end
41
+
42
+ # DELETE /account_emails/1
43
+ # DELETE /account_emails/1.json
44
+ def destroy
45
+ @account_email = AccountEmail.where(:account_id => params[:id], :email => params[:email])
46
+ @account_email.destroy
47
+
48
+ respond_to do |format|
49
+ format.html { redirect_to account_email_path(params[:id]) }
50
+ format.json { head :no_content }
51
+ end
52
+ end
53
+ end
54
+ end
@@ -20,6 +20,7 @@ class Kaui::AccountsController < Kaui::EngineController
20
20
 
21
21
  if @account.present? and @account.is_a? Kaui::Account
22
22
  @tags = Kaui::KillbillHelper::get_tags_for_account(@account.account_id).sort
23
+ @account_emails = Kaui::AccountEmail.where(:account_id => @account.account_id)
23
24
  @payment_methods = Kaui::KillbillHelper::get_payment_methods(@account.account_id)
24
25
  @bundles = Kaui::KillbillHelper::get_bundles(@account.account_id)
25
26
  @subscriptions_by_bundle_id = {}
@@ -127,4 +128,9 @@ class Kaui::AccountsController < Kaui::EngineController
127
128
  redirect_to :back
128
129
  end
129
130
 
131
+ def toggle_email_notifications
132
+ @account = Kaui::KillbillHelper::update_email_notifications(params[:id], params[:is_notified])
133
+ flash[:notice] = "Email preferences updated"
134
+ redirect_to :back
135
+ end
130
136
  end
@@ -7,7 +7,7 @@ module Kaui
7
7
 
8
8
  respond_to do |format|
9
9
  format.html # index.html.erb
10
- format.json { render json: @tag_definitions }
10
+ format.json { render :json => @tag_definitions }
11
11
  end
12
12
  end
13
13
 
@@ -18,7 +18,7 @@ module Kaui
18
18
 
19
19
  respond_to do |format|
20
20
  format.html # show.html.erb
21
- format.json { render json: @tag_definition }
21
+ format.json { render :json => @tag_definition }
22
22
  end
23
23
  end
24
24
 
@@ -29,7 +29,7 @@ module Kaui
29
29
 
30
30
  respond_to do |format|
31
31
  format.html # new.html.erb
32
- format.json { render json: @tag_definition }
32
+ format.json { render :json => @tag_definition }
33
33
  end
34
34
  end
35
35
 
@@ -45,11 +45,11 @@ module Kaui
45
45
 
46
46
  respond_to do |format|
47
47
  if @tag_definition.save
48
- format.html { redirect_to @tag_definition, notice: 'Tag definition was successfully created.' }
49
- format.json { render json: @tag_definition, status: :created, location: @tag_definition }
48
+ format.html { redirect_to @tag_definition, :notice => 'Tag definition was successfully created.' }
49
+ format.json { render :json => @tag_definition, :status => :created, :location => @tag_definition }
50
50
  else
51
- format.html { render action: "new" }
52
- format.json { render json: @tag_definition.errors, status: :unprocessable_entity }
51
+ format.html { render :action => "new" }
52
+ format.json { render :json => @tag_definition.errors, :status => :unprocessable_entity }
53
53
  end
54
54
  end
55
55
  end
@@ -61,11 +61,11 @@ module Kaui
61
61
 
62
62
  respond_to do |format|
63
63
  if @tag_definition.update_attributes(params[:tag_definition])
64
- format.html { redirect_to @tag_definition, notice: 'Tag definition was successfully updated.' }
64
+ format.html { redirect_to @tag_definition, :notice => 'Tag definition was successfully updated.' }
65
65
  format.json { head :no_content }
66
66
  else
67
- format.html { render action: "edit" }
68
- format.json { render json: @tag_definition.errors, status: :unprocessable_entity }
67
+ format.html { render :action => "edit" }
68
+ format.json { render :json => @tag_definition.errors, :status => :unprocessable_entity }
69
69
  end
70
70
  end
71
71
  end
@@ -0,0 +1,85 @@
1
+ module Kaui
2
+ class TagsController < ApplicationController
3
+ # GET /tags
4
+ # GET /tags.json
5
+ def index
6
+ @tags = Tag.all
7
+
8
+ respond_to do |format|
9
+ format.html # index.html.erb
10
+ format.json { render :json => @tags }
11
+ end
12
+ end
13
+
14
+ # GET /tags/1
15
+ # GET /tags/1.json
16
+ def show
17
+ @tag = Tag.find(params[:id])
18
+
19
+ respond_to do |format|
20
+ format.html # show.html.erb
21
+ format.json { render :json => @tag }
22
+ end
23
+ end
24
+
25
+ # GET /tags/new
26
+ # GET /tags/new.json
27
+ def new
28
+ @tag = Tag.new
29
+
30
+ respond_to do |format|
31
+ format.html # new.html.erb
32
+ format.json { render :json => @tag }
33
+ end
34
+ end
35
+
36
+ # GET /tags/1/edit
37
+ def edit
38
+ @tag = Tag.find(params[:id])
39
+ end
40
+
41
+ # POST /tags
42
+ # POST /tags.json
43
+ def create
44
+ @tag = Tag.new(params[:tag])
45
+
46
+ respond_to do |format|
47
+ if @tag.save
48
+ format.html { redirect_to @tag, :notice => 'Tag was successfully created.' }
49
+ format.json { render :json => @tag, :status => :created, :location => @tag }
50
+ else
51
+ format.html { render :action => "new" }
52
+ format.json { render :json => @tag.errors, :status => :unprocessable_entity }
53
+ end
54
+ end
55
+ end
56
+
57
+ # PUT /tags/1
58
+ # PUT /tags/1.json
59
+ def update
60
+ @tag = Tag.find(params[:id])
61
+
62
+ respond_to do |format|
63
+ if @tag.update_attributes(params[:tag])
64
+ format.html { redirect_to @tag, :notice => 'Tag was successfully updated.' }
65
+ format.json { head :no_content }
66
+ else
67
+ format.html { render :action => "edit" }
68
+ format.json { render :json => @tag.errors, :status => :unprocessable_entity }
69
+ end
70
+ end
71
+ end
72
+
73
+ # DELETE /tags/1
74
+ # DELETE /tags/1.json
75
+ def destroy
76
+ @tag = Tag.find(params[:id])
77
+ @tag.destroy
78
+
79
+ respond_to do |format|
80
+ format.html { redirect_to tags_url }
81
+ format.json { head :no_content }
82
+ end
83
+ end
84
+ end
85
+ end
@@ -72,6 +72,60 @@ module Kaui
72
72
  end
73
73
  end
74
74
 
75
+ def self.get_account_emails(account_id)
76
+ begin
77
+ data = call_killbill :get, "/1.0/kb/accounts/#{account_id}/emails"
78
+ process_response(data, :multiple) { |json| Kaui::AccountEmail.new(json) }
79
+ rescue => e
80
+ puts "#{$!}\n\t" + e.backtrace.join("\n\t")
81
+ end
82
+ end
83
+
84
+ def self.add_account_email(account_email, current_user = nil, reason = nil, comment = nil)
85
+ begin
86
+ account_email_data = Kaui::AccountEmail.camelize(account_email.to_hash)
87
+ data = call_killbill :post,
88
+ "/1.0/kb/accounts/#{account_email.account_id}/emails",
89
+ ActiveSupport::JSON.encode(account_email_data, :root => false),
90
+ :content_type => "application/json",
91
+ "X-Killbill-CreatedBy" => current_user,
92
+ "X-Killbill-Reason" => extract_reason_code(reason),
93
+ "X-Killbill-Comment" => "#{comment}"
94
+ return data[:code] = 201
95
+ rescue => e
96
+ puts "#{$!}\n\t" + e.backtrace.join("\n\t")
97
+ end
98
+ end
99
+
100
+ def self.remove_account_email(account_email, current_user = nil, reason = nil, comment = nil)
101
+ begin
102
+ data = call_killbill :delete,
103
+ "/1.0/kb/accounts/#{account_email.account_id}/emails/#{account_email.email}",
104
+ "X-Killbill-CreatedBy" => current_user,
105
+ "X-Killbill-Reason" => "#{reason}",
106
+ "X-Killbill-Comment" => "#{comment}"
107
+ return data[:code] < 300
108
+ rescue => e
109
+ puts "#{$!}\n\t" + e.backtrace.join("\n\t")
110
+ end
111
+ end
112
+
113
+ def self.update_email_notifications(account_id, is_notified, current_user = nil, reason = nil, comment = nil)
114
+ begin
115
+ email_data = { :isNotifiedForInvoices => is_notified }
116
+ data = call_killbill :put,
117
+ "/1.0/kb/accounts/#{account_id}/emailNotifications",
118
+ ActiveSupport::JSON.encode(email_data, :root => false),
119
+ :content_type => "application/json",
120
+ "X-Killbill-CreatedBy" => current_user,
121
+ "X-Killbill-Reason" => extract_reason_code(reason),
122
+ "X-Killbill-Comment" => "#{comment}"
123
+ return data[:code] = 200
124
+ rescue => e
125
+ puts "#{$!}\n\t" + e.backtrace.join("\n\t")
126
+ end
127
+ end
128
+
75
129
  ############## BUNDLE ##############
76
130
 
77
131
  def self.get_bundles(account_id)
@@ -15,7 +15,8 @@ class Kaui::Account < Kaui::Base
15
15
  define_attr :state
16
16
  define_attr :country
17
17
  define_attr :phone
18
- define_attr :balance
18
+ define_attr :balance
19
+ define_attr :is_notified_for_invoices
19
20
  has_one :bill_cycle_day, Kaui::BillCycleDay
20
21
 
21
22
  def initialize(data = {})
@@ -34,7 +35,8 @@ class Kaui::Account < Kaui::Base
34
35
  :country => data['country'],
35
36
  :phone => data['phone'],
36
37
  :bill_cycle_day => data['billCycleDay'],
37
- :balance => data['accountBalance'])
38
+ :balance => data['accountBalance'],
39
+ :is_notified_for_invoices => data['isNotifiedForInvoices'])
38
40
  end
39
41
 
40
42
  def to_param
@@ -0,0 +1,48 @@
1
+ class Kaui::AccountEmail < Kaui::Base
2
+
3
+ define_attr :account_id
4
+ define_attr :email
5
+
6
+ def initialize(attributes = {})
7
+ super(attributes)
8
+
9
+ # We make Rails believe the id for the AccountEmail is actually the account_id
10
+ # (this is handy for routes and form_for helpers). Make sure though to mark it as not persisted
11
+ # (even though @id is not nil)
12
+ @persisted = false
13
+ end
14
+
15
+ def self.where(conditions)
16
+ account_emails = Kaui::KillbillHelper.get_account_emails(conditions[:account_id]) || []
17
+ return account_emails.sort unless conditions[:email].present?
18
+
19
+ account_emails.each do |account_email|
20
+ return account_email if account_email.email == conditions[:email]
21
+ end
22
+ []
23
+ end
24
+
25
+ def save
26
+ success = Kaui::KillbillHelper.add_account_email(self)
27
+ @errors.add(:save, 'Unable to save the email') unless success
28
+ success
29
+ end
30
+
31
+ def destroy
32
+ success = Kaui::KillbillHelper.remove_account_email(self)
33
+ @errors.add(:destroy, 'Unable to destroy the email') unless success
34
+ success
35
+ end
36
+
37
+ def id
38
+ to_param
39
+ end
40
+
41
+ def to_param
42
+ @account_id
43
+ end
44
+
45
+ def <=>(account_email)
46
+ @email <=> account_email.email
47
+ end
48
+ end
@@ -0,0 +1,10 @@
1
+ <dt>Emails:</dt>
2
+ <dd>
3
+ <% if account_emails.present? %>
4
+ <% account_emails.each_with_index do |account_email, idx| %>
5
+ <span><%= account_email.email %><% if idx < account_emails.length - 1 %>, <% end %></span>
6
+ <% end %>
7
+ <br/>
8
+ <% end %>
9
+ <%= link_to 'Add/Remove', account_email_path(account_id), :class => 'btn btn-mini' %>
10
+ </dd>
@@ -0,0 +1,33 @@
1
+ <%= form_for(@account_email) do |f| %>
2
+ <% if @account_email.errors.any? %>
3
+ <div class='alert alert-error'>
4
+ <h2><%= pluralize(@account_email.errors.count, 'error') %> prohibited this account email from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @account_email.errors.full_messages.each do |msg| %>
8
+ <li><%= msg %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class='control-group'>
15
+ <%= f.label :account_id, 'Account Id:', :class => 'control-label' %>
16
+ <div class='controls'>
17
+ <%= f.text_field :account_id, :class => 'input-xlarge' %>
18
+ </div>
19
+ </div>
20
+ <div class='control-group'>
21
+ <%= f.label :email, 'Email:', :class => 'control-label' %>
22
+ <div class='controls'>
23
+ <%= f.text_field :email %>
24
+ </div>
25
+ </div>
26
+ <div class='form-actions'>
27
+ <%= f.submit @account_email.new_record? ? 'Create' : 'Update', :class => 'btn btn-primary' %>
28
+ <% unless @account_email.new_record? %>
29
+ <%= link_to 'Show', @account_email, :class => 'btn' %>
30
+ <% end %>
31
+ <%= link_to 'Back', :back, :class => 'btn' %>
32
+ </div>
33
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing account_email</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @account_email %> |
6
+ <%= link_to 'Back', account_emails_path %>
@@ -0,0 +1,3 @@
1
+ <h1>New account email</h1>
2
+
3
+ <%= render 'form' %>
@@ -0,0 +1,24 @@
1
+ <h2>Account emails</h2>
2
+
3
+ <table id='invoice_table' class='table table-condensed data-table'>
4
+ <thead>
5
+ <tr>
6
+ <th style='width: 45%'>Account id</th>
7
+ <th style='width: 45%'>Email</th>
8
+ <th style='width: 10%'></th>
9
+ </tr>
10
+ </thead>
11
+ <tbody>
12
+ <% @account_emails.each do |account_email| %>
13
+ <tr>
14
+ <td><%= link_to account_email.account_id, account_path(account_email.account_id) %></td>
15
+ <td><%= account_email.email %></td>
16
+ <td><%= link_to 'Destroy', account_email_path(account_email.account_id, :email => account_email.email), :confirm => 'Are you sure?', :method => :delete %></td>
17
+ </tr>
18
+ <% end %>
19
+ </tbody>
20
+ </table>
21
+
22
+ <br />
23
+
24
+ <%= link_to 'New Account email', new_account_email_path(:account_id => @account_id), :class => 'btn btn-primary' %>
@@ -38,9 +38,13 @@
38
38
  <% else %>
39
39
  <dd><span class="label label-important" ><%= @account.balance %></span>&nbsp;</dd>
40
40
  <% end %>
41
-
41
+ <dt>Notified for inv?</dt>
42
+ <dd><%= @account.is_notified_for_invoices %>&nbsp;<%= link_to "Toggle", kaui_engine.toggle_email_notifications_account_path(@account.account_id, :is_notified => !@account.is_notified_for_invoices), :method => :post, :class => 'btn btn-mini' %></dd>
43
+
42
44
  <%= render :partial => "kaui/tags/tags_table",
43
45
  :locals => { :tags => @tags, :tags_url_or_path => kaui_engine.edit_account_tags_path(:params => { :account_id => @account.account_id }) } %>
46
+ <%= render :partial => "kaui/account_emails/account_emails_table",
47
+ :locals => { :account_emails => @account_emails, :account_id => @account.account_id } %>
44
48
  </dl>
45
49
  <h2><%= link_to "Billing timeline", kaui_engine.account_timeline_path(@account.account_id) %></h2>
46
50
  <% if @payment_methods.present? %>