moneyrail 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -26,7 +26,7 @@ Jeweler::Tasks.new do |gemspec|
26
26
  gemspec.homepage = "http://github.com/yhara/#{PROJECT_NAME}"
27
27
  gemspec.description = gemspec.summary
28
28
  gemspec.authors = ["Yutaka HARA"]
29
- gemspec.add_dependency('rails', '= 2.3.4')
29
+ gemspec.add_dependency('rails', '= 2.3.5')
30
30
  gemspec.add_dependency('ruby-station-runtime', '>= 0.0.4')
31
31
  gemspec.add_dependency('less')
32
32
  gemspec.add_dependency('sqlite3-ruby')
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -1,9 +1,11 @@
1
1
  class HomeController < ApplicationController
2
2
 
3
3
  def index
4
- @months = []
5
- collect_months(@months)
6
- append_current_month(@months)
4
+ months = []
5
+ collect_months(months)
6
+ append_current_month(months)
7
+
8
+ @months = months.group_by(&:year)
7
9
  end
8
10
 
9
11
  private
@@ -1,56 +1,104 @@
1
1
  class StatsController < ApplicationController
2
- def show
2
+
3
+ def month
4
+ @mode = :month
5
+
3
6
  year = params[:year].to_i
4
7
  month = params[:month].to_i
5
-
6
8
  @month = Date.new(year, month)
7
- @month_range = @month .. ((@month >> 1) - 1)
9
+ @stat = make_month_stats(@month)
8
10
 
9
- @categories = Category.hashed.values_at(:expense, :income).flatten(1)
11
+ render :show
12
+ end
13
+
14
+ def year
15
+ @mode = :year
16
+
17
+ year = params[:year].to_i
18
+ @year = Date.new(year)
19
+ months = (0...12).map{|i| @year >> i}
20
+ @stat = make_year_stats(months)
10
21
 
11
- @stat = make_stats
22
+ render :show
23
+ end
24
+
25
+ def recent
26
+ @mode = :recent
27
+
28
+ now = DateTime.now.beginning_of_month
29
+ months = (0...12).map{|i| now << i}.reverse
30
+ @stat = make_year_stats(months)
31
+
32
+ render :show
12
33
  end
13
34
 
14
35
  private
15
36
 
16
- # Expense Income
17
- # cat1 cat2 cat3 cat4 cat5 cat6
18
- # acc1 99 99 99 0 0 33
19
- # acc2 12 34 567 8 9 0
37
+ def month_range(month)
38
+ month.beginning_of_month .. month.end_of_month
39
+ end
40
+
41
+ # - Expense Income
42
+ # - cat1 cat2 cat3 cat4 sum cat5 cat6 sum
43
+ # acc1 99 99 99 0 x 0 33 y
44
+ # acc2 12 34 567 8 x 9 0 y
20
45
  # ------------------------------------
21
46
  # sum
22
- def make_stats
23
- items = Item.all(:conditions => {:date => @month_range, :type => ["Expense", "Income"]}).group_by(&:category)
47
+ def make_month_stats(month)
48
+ rows = Account.all(:order => "position").map{|account|
49
+ items = Item.all(:conditions => {
50
+ :account_id => account.id,
51
+ :date => month_range(month),
52
+ :type => ["Expense", "Income"]
53
+ }).group_by(&:category)
24
54
 
25
- accounts = Account.all(:order => "position")
55
+ make_row(account.name, items)
56
+ }
57
+
58
+ return rows.push make_sum_row(rows)
59
+ end
60
+
61
+ # - Expense Income
62
+ # month cat1 cat2 cat3 cat4 cat5 cat6
63
+ # 200901 99 99 99 0 0 33
64
+ # 200902 12 34 567 8 9 0
65
+ # ------------------------------------
66
+ # sum
67
+ def make_year_stats(months)
68
+ rows = months.map{|month|
69
+ items = Item.all(:conditions => {
70
+ :date => month_range(month),
71
+ :type => ["Expense", "Income"]
72
+ }).group_by(&:category)
26
73
 
27
- rows = accounts.map{|account|
28
- make_stat_row(account, items)
74
+ make_row(month, items)
29
75
  }
30
76
 
31
77
  return rows.push make_sum_row(rows)
32
78
  end
33
79
 
34
- def make_stat_row(account, items)
35
- row = @categories.map{|category|
36
- if items[category]
37
- items[category].
38
- find_all{|item| item.account == account}.
39
- map{|item| item.amount}.
40
- sum
41
- else
42
- 0
43
- end
80
+ def make_row(header, items)
81
+ expenses = Category.expenses.map{|category|
82
+ (items[category] || []).map(&:amount).sum
83
+ }
84
+ incomes = Category.incomes.map{|category|
85
+ (items[category] || []).map(&:amount).sum
44
86
  }
45
- row.unshift(account.name)
87
+
88
+ [header] +
89
+ expenses +
90
+ [expenses.sum] +
91
+ incomes +
92
+ [incomes.sum] +
93
+ [incomes.sum - expenses.sum]
46
94
  end
47
95
 
48
96
  def make_sum_row(rows)
49
97
  rows.transpose.map{|cells|
50
- if cells.first.class == String
51
- ""
52
- else
98
+ if cells.first.is_a? Numeric
53
99
  cells.sum
100
+ else
101
+ "sum"
54
102
  end
55
103
  }
56
104
  end
@@ -8,6 +8,10 @@ module ApplicationHelper
8
8
  d.strftime("%Y-%m")
9
9
  end
10
10
 
11
+ def format_year(d)
12
+ d.strftime("%Y")
13
+ end
14
+
11
15
  # (str, date) or (date)
12
16
  def link_to_edit_logs(a, b=nil)
13
17
  str, date = str_and_date(a, b)
@@ -21,10 +25,16 @@ module ApplicationHelper
21
25
  link_to str, show_logs_path(:year => date.year, :month => date.month)
22
26
  end
23
27
 
24
- def link_to_show_stats(a, b=nil)
28
+ def link_to_show_month_stats(a, b=nil)
25
29
  str, date = str_and_date(a, b)
26
30
 
27
- link_to str, show_stats_path(:year => date.year, :month => date.month)
31
+ link_to str, show_month_stats_path(:year => date.year, :month => date.month)
32
+ end
33
+
34
+ def link_to_show_year_stats(a, b=nil)
35
+ str, date = b ? [a, b] : [format_year(a), a]
36
+
37
+ link_to str, show_year_stats_path(:year => date.year)
28
38
  end
29
39
 
30
40
  private
@@ -21,4 +21,12 @@ class Category < ActiveRecord::Base
21
21
  }
22
22
  end
23
23
 
24
+ def self.incomes
25
+ self.all(:conditions => {:kind => "Income"})
26
+ end
27
+
28
+ def self.expenses
29
+ self.all(:conditions => {:kind => "Expense"})
30
+ end
31
+
24
32
  end
@@ -3,15 +3,22 @@
3
3
  <li><%= link_to 'Categories', categories_path %></li>
4
4
  <li><%= link_to 'Items', items_path %></li>
5
5
  </ul>
6
+
7
+ <h2>Logs</h2>
8
+ <%= link_to 'Recent stats', show_recent_stats_path %>
9
+
10
+ <% @months.each do |year, months| %>
11
+ <h3><%= link_to_show_year_stats(Date.new(year)) %></h3>
6
12
  <ul>
7
- <% @months.each do |m| %>
13
+ <% months.each do |m| %>
8
14
  <li>
9
15
  <%= link_to "Logs: #{m.year}-#{m.month}",
10
16
  show_logs_path(:year => m.year, :month => m.month) %>
11
17
  <%= link_to "(edit)",
12
18
  edit_logs_path(:year => m.year, :month => m.month) %>
13
19
  <%= link_to "stats",
14
- show_stats_path(:year => m.year, :month => m.month) %>
20
+ show_month_stats_path(:year => m.year, :month => m.month) %>
15
21
  </li>
16
22
  <% end %>
17
23
  </ul>
24
+ <% end %>
@@ -7,7 +7,9 @@
7
7
 
8
8
  <%= stylesheet_link_tag 'scaffold' %>
9
9
 
10
- <% if controller.controller_name == "logs" %>
10
+ <% case controller.controller_name
11
+ when "logs" %>
12
+
11
13
  <title>MoneyRail : Logs: <%=h @year %>/<%=h @month %></title>
12
14
  <%= stylesheet_link_tag 'editor' %>
13
15
 
@@ -17,6 +19,10 @@
17
19
  <%= javascript_include_tag 'jquery' %>
18
20
  <%= javascript_include_tag 'editor' %>
19
21
  <% end %>
22
+
23
+ <% when "stats" %>
24
+ <%= stylesheet_link_tag 'stats' %>
25
+
20
26
  <% else %>
21
27
  <title>MoneyRail :
22
28
  <%=h controller.controller_name.camelcase %>:
@@ -1,21 +1,48 @@
1
1
  <h2>Statictics
2
- <small><%= link_to_show_stats("<<", @month << 1) %></small>
3
- <%= format_month(@month) %>
4
- <small><%= link_to_show_stats(">>", @month >> 1) %></small>
2
+ <% case @mode ; when :year %>
3
+ <small><%= link_to_show_year_stats("<<", @year-365) %></small>
4
+ <%= format_year(@year) %>
5
+ <small><%= link_to_show_year_stats(">>", @year+365) %></small>
6
+ <% when :month %>
7
+ <small><%= link_to_show_month_stats("<<", @month << 1) %></small>
8
+ <%= format_month(@month) %>
9
+ <small><%= link_to_show_month_stats(">>", @month >> 1) %></small>
10
+ <% when :recent %>
11
+ (recent 12 months)
12
+ <% end %>
5
13
  </h2>
6
14
 
7
- <table>
15
+ <table class="stats">
16
+ <%# row 1: Expense and Income %>
17
+ <tr>
18
+ <th rowspan="2"></th>
19
+ <th colspan="<%= Category.expenses.size + 1 %>">Expense</th>
20
+ <th colspan="<%= Category.incomes.size + 1 %>">Income</th>
21
+ <th rowspan="2">sum</th>
22
+ </tr>
23
+
24
+ <%# row 2: Categories %>
8
25
  <tr>
9
- <th>account</th>
10
- <% @categories.each do |category| %>
26
+ <% Category.expenses.each do |category| %>
27
+ <th><%=h category.name %></th>
28
+ <% end %>
29
+ <th>sum</th>
30
+ <% Category.incomes.each do |category| %>
11
31
  <th><%=h category.name %></th>
12
32
  <% end %>
33
+ <th>sum</th>
13
34
  </tr>
14
35
 
36
+ <%# row 3-: stat %>
15
37
  <% @stat.each do |row| %>
16
38
  <tr>
17
- <% row.each do |cell| %>
18
- <td><%=h cell%></td>
39
+ <% row.each do |item| %>
40
+ <% case item
41
+ when Date %>
42
+ <td><%= link_to_show_month_stats(item) %></td>
43
+ <% else %>
44
+ <td><%=h item %></td>
45
+ <% end %>
19
46
  <% end %>
20
47
  </tr>
21
48
  <% end %>
@@ -1,7 +1,7 @@
1
1
  # Be sure to restart your server when you modify this file
2
2
 
3
3
  # Specifies gem version of Rails to use when vendor/rails is not present
4
- RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION
4
+ RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION
5
5
 
6
6
  # Bootstrap the Rails environment, frameworks, and default configuration
7
7
  require File.join(File.dirname(__FILE__), 'boot')
data/config/routes.rb CHANGED
@@ -35,8 +35,12 @@ ActionController::Routing::Routes.draw do |map|
35
35
  :controller => :logs, :action => 'view', :mode => :edit
36
36
 
37
37
  # -- statistics
38
- map.show_stats 'stats/:year/:month',
39
- :controller => :stats, :action => 'show'
38
+ map.show_recent_stats 'stats/recent',
39
+ :controller => :stats, :action => 'recent'
40
+ map.show_month_stats 'stats/:year/:month',
41
+ :controller => :stats, :action => 'month'
42
+ map.show_year_stats 'stats/:year',
43
+ :controller => :stats, :action => 'year'
40
44
 
41
45
  # The priority is based upon order of creation: first created -> highest priority.
42
46
 
data/moneyrail.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{moneyrail}
8
- s.version = "0.1.4"
8
+ s.version = "0.1.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Yutaka HARA"]
12
- s.date = %q{2009-12-06}
12
+ s.date = %q{2010-02-09}
13
13
  s.description = %q{Household account book, written in Rails}
14
14
  s.email = %q{yutaka.hara/at/gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -102,6 +102,7 @@ Gem::Specification.new do |s|
102
102
  "public/robots.txt",
103
103
  "public/stylesheets/editor.less",
104
104
  "public/stylesheets/scaffold.css",
105
+ "public/stylesheets/stats.less",
105
106
  "script/about",
106
107
  "script/autospec",
107
108
  "script/console",
@@ -156,7 +157,10 @@ Gem::Specification.new do |s|
156
157
  "spec/models/category_spec.rb",
157
158
  "spec/models/income_spec.rb",
158
159
  "spec/models/item_spec.rb",
159
- "spec/spec_helper.rb"
160
+ "spec/spec_helper.rb",
161
+ "test/functional/notes_controller_test.rb",
162
+ "test/unit/helpers/notes_helper_test.rb",
163
+ "test/unit/note_test.rb"
160
164
  ]
161
165
 
162
166
  if s.respond_to? :specification_version then
@@ -164,18 +168,18 @@ Gem::Specification.new do |s|
164
168
  s.specification_version = 3
165
169
 
166
170
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
167
- s.add_runtime_dependency(%q<rails>, ["= 2.3.4"])
171
+ s.add_runtime_dependency(%q<rails>, ["= 2.3.5"])
168
172
  s.add_runtime_dependency(%q<ruby-station-runtime>, [">= 0.0.4"])
169
173
  s.add_runtime_dependency(%q<less>, [">= 0"])
170
174
  s.add_runtime_dependency(%q<sqlite3-ruby>, [">= 0"])
171
175
  else
172
- s.add_dependency(%q<rails>, ["= 2.3.4"])
176
+ s.add_dependency(%q<rails>, ["= 2.3.5"])
173
177
  s.add_dependency(%q<ruby-station-runtime>, [">= 0.0.4"])
174
178
  s.add_dependency(%q<less>, [">= 0"])
175
179
  s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
176
180
  end
177
181
  else
178
- s.add_dependency(%q<rails>, ["= 2.3.4"])
182
+ s.add_dependency(%q<rails>, ["= 2.3.5"])
179
183
  s.add_dependency(%q<ruby-station-runtime>, [">= 0.0.4"])
180
184
  s.add_dependency(%q<less>, [">= 0"])
181
185
  s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
@@ -0,0 +1,13 @@
1
+ table.stats {
2
+ border-collapse: collapse;
3
+
4
+ td, th {
5
+ border: 1px solid black;
6
+ padding: 0.2em;
7
+ }
8
+
9
+ td {
10
+ text-align: right;
11
+ }
12
+
13
+ }
@@ -0,0 +1,45 @@
1
+ require 'test_helper'
2
+
3
+ class NotesControllerTest < ActionController::TestCase
4
+ test "should get index" do
5
+ get :index
6
+ assert_response :success
7
+ assert_not_nil assigns(:notes)
8
+ end
9
+
10
+ test "should get new" do
11
+ get :new
12
+ assert_response :success
13
+ end
14
+
15
+ test "should create note" do
16
+ assert_difference('Note.count') do
17
+ post :create, :note => { }
18
+ end
19
+
20
+ assert_redirected_to note_path(assigns(:note))
21
+ end
22
+
23
+ test "should show note" do
24
+ get :show, :id => notes(:one).to_param
25
+ assert_response :success
26
+ end
27
+
28
+ test "should get edit" do
29
+ get :edit, :id => notes(:one).to_param
30
+ assert_response :success
31
+ end
32
+
33
+ test "should update note" do
34
+ put :update, :id => notes(:one).to_param, :note => { }
35
+ assert_redirected_to note_path(assigns(:note))
36
+ end
37
+
38
+ test "should destroy note" do
39
+ assert_difference('Note.count', -1) do
40
+ delete :destroy, :id => notes(:one).to_param
41
+ end
42
+
43
+ assert_redirected_to notes_path
44
+ end
45
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class NotesHelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class NoteTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moneyrail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yutaka HARA
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-06 00:00:00 +09:00
12
+ date: 2010-02-09 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - "="
22
22
  - !ruby/object:Gem::Version
23
- version: 2.3.4
23
+ version: 2.3.5
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: ruby-station-runtime
@@ -147,6 +147,7 @@ files:
147
147
  - public/robots.txt
148
148
  - public/stylesheets/editor.less
149
149
  - public/stylesheets/scaffold.css
150
+ - public/stylesheets/stats.less
150
151
  - script/about
151
152
  - script/autospec
152
153
  - script/console
@@ -224,3 +225,6 @@ test_files:
224
225
  - spec/models/income_spec.rb
225
226
  - spec/models/item_spec.rb
226
227
  - spec/spec_helper.rb
228
+ - test/functional/notes_controller_test.rb
229
+ - test/unit/helpers/notes_helper_test.rb
230
+ - test/unit/note_test.rb