Skip to content

Commit

Permalink
Merge branch 'master' of github.com:dodo-as/dodo
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikola Trandafilovic committed Nov 8, 2011
2 parents 7de48cf + d7f6b04 commit efd0b9c
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 40 deletions.
26 changes: 16 additions & 10 deletions app/models/admin_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ class AdminLog < ActiveRecord::Base

validates :table_name, :old_value, :new_value, :admin_id, :presence=> true

# This static method returns a function suitable fo use as an around_filter
# which will log actions.
# 'field' is the name of a property whose value should be logged on change.
def self.log field
return Proc.new {|controller, action|


user = controller.me

if !user
puts "UNKNOWN ADMIN USER! HALP!!!!!!"
crash
end

obj = controller.send(field)

before = "null"
Expand All @@ -33,20 +29,30 @@ def self.log field
end

table = obj.class.name

puts "TRALALALALALA", user, before, after

if before != after
puts "WEEE SAVELISAVE SAVE SAVE SAVE"
l = AdminLog.new({:admin => user, :old_value => before, :new_value => after, :table_name => table})
l.save
puts l.errors
end
}
end

# Return a list of hashes. every hash has three items, :field, which is a field name,
# :before, which is the old value, and :after is the new value. This list includes only
# the changed fields.
def changes
return Log.diff self.old_value, self.new_value
end

# Return a list of hashes. every hash has three items, :field, which is a field name,
# :before, which is the old value, and :after is the new value
def data
before = Log.handle_json self.old_value
after = Log.handle_json self.new_value
fields = before.keys | after.keys
return fields.map {|key| { :field => key, :before => before[key], :after => after[key] } }
end


end
54 changes: 34 additions & 20 deletions app/models/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ class Log < ActiveRecord::Base

validates :table_name, :old_value, :new_value, :user_id, :company_id, :presence=> true

# Convert the specified model isntance to JSON. If the object has a log_include property,
# include the foreign keys returned by that property
def self.my_json obj
inc = obj.log_include if obj.respond_to?(:log_include) else []
return obj.to_json :include => inc
end


# This static method returns a function suitable fo use as an around_filter
# which will log actions.
# 'field' is the name of a property whose value should be logged on change.
def self.log field
return Proc.new {|controller, action|

user = controller.me
company = user.current_company
# puts "AFSDDDDDDD", controller, field
obj = controller.send(field)

before = "null"
Expand Down Expand Up @@ -43,13 +47,16 @@ def self.log field
}
end

# Return a list of hashes. every hash has three items, :field, which is a field name,
# :before, which is the old value, and :after is the new value. This list includes only
# the changed fields.
def changes
puts "AAAAAAAA", self[0]
return Log.diff self.old_value, self.new_value
end

def self.diff old_value, new_value

# Parse a JSON string and flatten dicts and lists into a single hash
def self.handle_json text

def self.my_flatten_internal source, destination, prefix
source.each { |key, value|
if key != "updated_at"
Expand All @@ -58,9 +65,7 @@ def self.my_flatten_internal source, destination, prefix
else
if value.class == Array
valhash = Hash[value.map {|it| ["element" + it["id"].to_s, it] }]
puts "WEEE", value, "=>", valhash, "YAY"
my_flatten_internal valhash, destination, prefix + key + " "
# puts destination
else
destination[prefix+key] = value
end
Expand All @@ -74,20 +79,19 @@ def self.my_flatten source
return my_flatten_internal source, {}, ""
end

before = ActiveSupport::JSON.decode(old_value)
after = ActiveSupport::JSON.decode(new_value)

if before
before = my_flatten before.values.first
else
before = {}
data = ActiveSupport::JSON.decode text
if data
return my_flatten data.values.first
end
return {}
end

# Helper method for the changes method.
# Refactored out of changes so that it can be used by AdminLog.changes as well.
def self.diff old_value, new_value

if after
after = my_flatten after.values.first
else
after = {}
end
before = Log.handle_json old_value
after = Log.handle_json new_value

def self.my_filter k1, k2
if !k1 && !k2
Expand All @@ -101,5 +105,15 @@ def self.my_filter k1, k2
fields = fields.select {|key| my_filter(before[key], after[key])}
return fields.map {|key| { :field => key, :before => before[key], :after => after[key] } }
end


# Return a list of hashes. every hash has three items, :field, which is a field name,
# :before, which is the old value, and :after is the new value
def data
before = Log.handle_json self.old_value
after = Log.handle_json self.new_value
fields = before.keys | after.keys
return fields.map {|key| { :field => key, :before => before[key], :after => after[key] } }
end


end
6 changes: 3 additions & 3 deletions app/models/period.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class Period < ActiveRecord::Base
cattr_reader :per_page
@@per_page = 200

STATUSES = {0 => 'New', 1 => 'Open', 2 => 'Done', 3 => 'Closed'}
STATUSE_NAMES = {'New' => 0, 'Open' => 1, 'Done' => 2, 'Closed' => 3}
STATUSES = {0 => 'New', 1 => 'Open', 2 => 'Done', 3 => 'Locked', 4 => 'Closed'}
STATUSE_NAMES = {'New' => 0, 'Open' => 1, 'Done' => 2, 'Locked' => 3, 'Closed' => 4}

# options: from_year, from_nr, to_year, to_nr, company_id
# options: from_year, from_nr, to_year, to_nr, company_id
# if (from_year, to_year) are nil, it will return all periods of the company from year 1900 to year 3999
# if (to_year) is nil, it will return all periods until last period of the company until year 3999
# if (from_year) is nil, it will return all periods since first period of the company since year 1900
Expand Down
24 changes: 22 additions & 2 deletions app/views/admin/admin_logs/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
<th>User</th>
<th>Created at</th>
<th>Changes</th>
<th></th>
</tr>

<% @admin_logs.each do |log| %>
<tr>
<td><%= log.table_name %></td>
<td><%= log.admin %></td>
<td><%= log.created_at %></td>
<td>
<table>
<td class="log-<%= log.id %>">

<table class="log-toggle">
<tr>
<th><%= t('logs.field') %></th>
<th><%= t('logs.before') %></th>
Expand All @@ -30,6 +32,24 @@
</tr>
<% end %>
</table>

<table class="log-toggle" style="display: none;">
<tr>
<th><%= t('logs.field') %></th>
<th><%= t('logs.before') %></th>
<th><%= t('logs.after') %></th>
</tr>
<% log.data.each do |change| %>
<tr>
<td><%= change[:field] %></td>
<td><%= change[:before] || t('logs.unset') %></td>
<td><%= change[:after] || t('logs.unset') %></td>
</tr>
<% end %>
</table>
</td>
<td>
<button type='button' onclick="javascript:$('.log-<%= log.id %> .log-toggle').toggle()">Toggle full change</button>
</td>
</tr>
<% end %>
Expand Down
25 changes: 23 additions & 2 deletions app/views/logs/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
<th>User</th>
<th>Created at</th>
<th>Changes</th>
<th></th>
</tr>

<% @logs.each do |log| %>
<tr>
<td><%= log.table_name %></td>
<td><%= log.user %></td>
<td><%= log.created_at %></td>
<td>
<table>
<td class="log-<%= log.id %>">

<table class="log-toggle">
<tr>
<th><%= t('logs.field') %></th>
<th><%= t('logs.before') %></th>
Expand All @@ -30,6 +32,25 @@
</tr>
<% end %>
</table>

<table class="log-toggle" style="display: none;">
<tr>
<th><%= t('logs.field') %></th>
<th><%= t('logs.before') %></th>
<th><%= t('logs.after') %></th>
</tr>
<% log.data.each do |change| %>
<tr>
<td><%= change[:field] %></td>
<td><%= change[:before] || t('logs.unset') %></td>
<td><%= change[:after] || t('logs.unset') %></td>
</tr>
<% end %>
</table>

</td>
<td>
<button type='button' onclick="javascript:$('.log-<%= log.id %> .log-toggle').toggle()">Toggle full change</button>
</td>
</tr>
<% end %>
Expand Down
8 changes: 5 additions & 3 deletions app/views/periods/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<th>New</th>
<th>Open</th>
<th>Done</th>
<th>Locked</th>
<th>Closed</th>
<th>Open bills</th>
<th>Bills</th>
Expand All @@ -24,15 +25,16 @@
<tr>
<td><%= period.year %></td>
<td><%= period.nr %></td>

<% for status in (0..period.status - 1) %>
<td></td>
<% end %>
<% if period.status < 3 then %>
<% end %>
<% if period.status < 4 then %>
<td><%= link_to '-&gt;'.html_safe, elevate_status_period_path(period), :method => :post %></td>
<% else %>
<td>X</td>
<% end %>
<% for status in (period.status+1..3) %>
<% for status in (period.status+1..4) %>
<td></td>
<% end %>
<td><%= period.open_bills.count %></td>
Expand Down
5 changes: 5 additions & 0 deletions config/authorization_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@
if_attribute :period => { :status => [ Period::STATUSE_NAMES['Open'], Period::STATUSE_NAMES['Done'] ] }
end

has_permission_on :journals, :to => :manage, :join_by => :and do
if_attribute :company_id => is {user.current_company.id}
if_attribute :period => { :status => [ Period::STATUSE_NAMES['Locked']] }
end

has_permission_on :bills, :to => :manage, :join_by => :and do
if_attribute :company_id => is {user.current_company.id}
if_attribute :period => { :status => [ Period::STATUSE_NAMES['Open'], Period::STATUSE_NAMES['Done'] ] }
Expand Down

0 comments on commit efd0b9c

Please sign in to comment.