Skip to content

Commit

Permalink
Implement SalaryItem as habt model between SalaryTemplate and SalaryB…
Browse files Browse the repository at this point in the history
…ookingTemplate.
  • Loading branch information
huerlisi committed Jan 18, 2012
1 parent 327532a commit ee4c82c
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 6 deletions.
15 changes: 15 additions & 0 deletions app/controllers/salary_templates_controller.rb
Expand Up @@ -8,4 +8,19 @@ def copy

render 'edit'
end

# has_many :salary_items
def new_salary_item
if salary_template_id = params[:id]
@salary_template = resource_class.find(salary_template_id)
else
@salary_template = resource_class.new
end

@salary_item = @salary_template.salary_items.build(
:times => 1
)

respond_with @salary_item
end
end
10 changes: 8 additions & 2 deletions app/models/salary.rb
Expand Up @@ -89,9 +89,15 @@ def salary_template

# Line Items
def build_line_items
salary_template.salary_booking_templates.each do |booking_template|
salary_template.salary_items.each do |salary_item|
line_item = line_items.build(:date => self.value_date)
line_item.set_booking_template(booking_template)

# Defaults from booking_template
line_item.set_booking_template(salary_item.salary_booking_template)

# Overrides from salary_item
line_item.times = salary_item.times if salary_item.times.present?
line_item.price = salary_item.price if salary_item.price.present?
end
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/salary_item.rb
@@ -0,0 +1,4 @@
class SalaryItem < ActiveRecord::Base
belongs_to :salary_template
belongs_to :salary_booking_template
end
5 changes: 4 additions & 1 deletion app/models/salary_template.rb
@@ -1,4 +1,7 @@
class SalaryTemplate < ActiveRecord::Base
belongs_to :person
has_and_belongs_to_many :salary_booking_templates

# Salary Items
has_many :salary_items
accepts_nested_attributes_for :salary_items, :allow_destroy => true
end
7 changes: 7 additions & 0 deletions app/views/salary_items/_form.html.haml
@@ -0,0 +1,7 @@
%tr.salary_item.nested-form-item
%td= salary_item.select :salary_booking_template_id, SalaryBookingTemplate.all.map{|template| [template.title, template.id]}, :required => true, :style => 'width: 30em'
%td= salary_item.text_field :times, :required => true, :style => 'width: 3em', :autocomplete => "off"
%td= salary_item.text_field :price, :required => true, :style => 'width: 5em', :autocomplete => "off"
%td.action_links
= salary_item.hidden_field :_destroy
= link_to t_action(:delete), '#', :class => 'icon-delete-text delete-nested-form-item'
6 changes: 6 additions & 0 deletions app/views/salary_items/_line_item.html.haml
@@ -0,0 +1,6 @@
%tr[line_item]
%td= line_item.date
%td= line_item.title
%td.currency= line_item.times_to_s
%td.currency= currency_fmt(line_item.price)
%td.currency= currency_fmt(line_item.total_amount)
7 changes: 7 additions & 0 deletions app/views/salary_items/_list_form.html.haml
@@ -0,0 +1,7 @@
= f.inputs t_title(:list, SalaryItem) do
%table#salary_items
%thead
= render 'salary_items/list_header'
%tbody
= f.fields_for :salary_items do |salary_item|
= render 'salary_items/form', :salary_item => salary_item
6 changes: 6 additions & 0 deletions app/views/salary_items/_list_header.html.haml
@@ -0,0 +1,6 @@
%tr
%th{:style => 'padding: 2px'}= t_attr(:salary_booking_template, SalaryItem)
%th{:style => 'padding: 2px; width: 3em'}= t_attr(:times, SalaryItem)
%th{:style => 'padding: 2px; width: 3em'}= t_attr(:price, SalaryItem)
%th
%th.action_links= link_to t_action(:more), [:new_salary_item, resource.persisted? ? resource : resource.class.name.underscore.pluralize.to_sym], :remote => true, :class => 'icon-add-text'
4 changes: 2 additions & 2 deletions app/views/salary_templates/_form.html.haml
Expand Up @@ -4,10 +4,10 @@
.row
.span8= f.input :title, :input_html => {"data-autofocus" => true}
.span8= f.input :person, :collection => Employee.all
.row
.span16= f.input :salary_booking_templates, :input_html => {:class => 'span12'}
.row
.span16= f.input :remarks, :input_html => {:rows => 2, :class => 'span12'}

= render 'salary_items/list_form', :f => f

= f.buttons do
= f.commit_button
3 changes: 3 additions & 0 deletions app/views/salary_templates/_salary_item_form.html.haml
@@ -0,0 +1,3 @@
= fields_for @salary_template do |salary_template|
= salary_template.fields_for :salary_items, @salary_item, :child_index => -(DateTime.now.to_i) do |salary_item|
= render 'salary_items/form', :salary_item => salary_item
2 changes: 2 additions & 0 deletions app/views/salary_templates/new_salary_item.js.erb
@@ -0,0 +1,2 @@
$('#salary_items tbody').append('<%=j render 'salary_item_form' %>');
initializeBehaviours();
7 changes: 7 additions & 0 deletions config/locales/bookyt_salary.de.yml
Expand Up @@ -27,6 +27,10 @@ de:
salary_booking_templates: Lohnarten
title: Titel
remarks: Bemerkungen
salary_item:
salary_booking_template: Lohnart
times: Anzahl
price: Betrag

salaries:
select_employee:
Expand All @@ -37,3 +41,6 @@ de:
salary_templates:
index:
title: Lohnvorlagen
salary_items:
list:
title: Lohnarten
6 changes: 5 additions & 1 deletion config/routes.rb
Expand Up @@ -6,7 +6,11 @@
end
end

resources :salary_templates
resources :salary_templates do
member do
get :new_salary_item
end
end

resources :salaries do
collection do
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/20120118121223_create_salary_items.rb
@@ -0,0 +1,15 @@
class CreateSalaryItems < ActiveRecord::Migration
def change
create_table :salary_items do |t|
t.integer :salary_booking_template_id
t.integer :salary_template_id
t.decimal :times, :precision => 10, :scale => 2
t.decimal :price, :precision => 10, :scale => 2

t.timestamps
end

add_index :salary_items, :salary_booking_template_id
add_index :salary_items, :salary_template_id
end
end
10 changes: 10 additions & 0 deletions spec/factories/salary_items.rb
@@ -0,0 +1,10 @@
# Read about factories at http://github.com/thoughtbot/factory_girl

FactoryGirl.define do
factory :salary_item do
salary_booking_template_id 1
salary_template_id 1
times "9.99"
price "9.99"
end
end
5 changes: 5 additions & 0 deletions spec/models/salary_item_spec.rb
@@ -0,0 +1,5 @@
require 'spec_helper'

describe SalaryItem do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit ee4c82c

Please sign in to comment.