From ee4c82c75df225b7bd87b21aef3d436865a14c68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20H=C3=BCrlimann=20=28CyT=29?= Date: Wed, 18 Jan 2012 15:00:50 +0100 Subject: [PATCH] Implement SalaryItem as habt model between SalaryTemplate and SalaryBookingTemplate. --- app/controllers/salary_templates_controller.rb | 15 +++++++++++++++ app/models/salary.rb | 10 ++++++++-- app/models/salary_item.rb | 4 ++++ app/models/salary_template.rb | 5 ++++- app/views/salary_items/_form.html.haml | 7 +++++++ app/views/salary_items/_line_item.html.haml | 6 ++++++ app/views/salary_items/_list_form.html.haml | 7 +++++++ app/views/salary_items/_list_header.html.haml | 6 ++++++ app/views/salary_templates/_form.html.haml | 4 ++-- .../salary_templates/_salary_item_form.html.haml | 3 +++ app/views/salary_templates/new_salary_item.js.erb | 2 ++ config/locales/bookyt_salary.de.yml | 7 +++++++ config/routes.rb | 6 +++++- db/migrate/20120118121223_create_salary_items.rb | 15 +++++++++++++++ spec/factories/salary_items.rb | 10 ++++++++++ spec/models/salary_item_spec.rb | 5 +++++ 16 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 app/models/salary_item.rb create mode 100644 app/views/salary_items/_form.html.haml create mode 100644 app/views/salary_items/_line_item.html.haml create mode 100644 app/views/salary_items/_list_form.html.haml create mode 100644 app/views/salary_items/_list_header.html.haml create mode 100644 app/views/salary_templates/_salary_item_form.html.haml create mode 100644 app/views/salary_templates/new_salary_item.js.erb create mode 100644 db/migrate/20120118121223_create_salary_items.rb create mode 100644 spec/factories/salary_items.rb create mode 100644 spec/models/salary_item_spec.rb diff --git a/app/controllers/salary_templates_controller.rb b/app/controllers/salary_templates_controller.rb index f8e66a4..313b9fb 100644 --- a/app/controllers/salary_templates_controller.rb +++ b/app/controllers/salary_templates_controller.rb @@ -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 diff --git a/app/models/salary.rb b/app/models/salary.rb index ef49839..3cdc754 100644 --- a/app/models/salary.rb +++ b/app/models/salary.rb @@ -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 diff --git a/app/models/salary_item.rb b/app/models/salary_item.rb new file mode 100644 index 0000000..57244cc --- /dev/null +++ b/app/models/salary_item.rb @@ -0,0 +1,4 @@ +class SalaryItem < ActiveRecord::Base + belongs_to :salary_template + belongs_to :salary_booking_template +end diff --git a/app/models/salary_template.rb b/app/models/salary_template.rb index bad3bcb..487db8d 100644 --- a/app/models/salary_template.rb +++ b/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 diff --git a/app/views/salary_items/_form.html.haml b/app/views/salary_items/_form.html.haml new file mode 100644 index 0000000..3a16e87 --- /dev/null +++ b/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' diff --git a/app/views/salary_items/_line_item.html.haml b/app/views/salary_items/_line_item.html.haml new file mode 100644 index 0000000..12ad416 --- /dev/null +++ b/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) diff --git a/app/views/salary_items/_list_form.html.haml b/app/views/salary_items/_list_form.html.haml new file mode 100644 index 0000000..3c4f9df --- /dev/null +++ b/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 diff --git a/app/views/salary_items/_list_header.html.haml b/app/views/salary_items/_list_header.html.haml new file mode 100644 index 0000000..af8f0f3 --- /dev/null +++ b/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' diff --git a/app/views/salary_templates/_form.html.haml b/app/views/salary_templates/_form.html.haml index 3cce5f0..4ad1a94 100644 --- a/app/views/salary_templates/_form.html.haml +++ b/app/views/salary_templates/_form.html.haml @@ -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 diff --git a/app/views/salary_templates/_salary_item_form.html.haml b/app/views/salary_templates/_salary_item_form.html.haml new file mode 100644 index 0000000..dc5dc78 --- /dev/null +++ b/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 diff --git a/app/views/salary_templates/new_salary_item.js.erb b/app/views/salary_templates/new_salary_item.js.erb new file mode 100644 index 0000000..f19d65e --- /dev/null +++ b/app/views/salary_templates/new_salary_item.js.erb @@ -0,0 +1,2 @@ +$('#salary_items tbody').append('<%=j render 'salary_item_form' %>'); +initializeBehaviours(); diff --git a/config/locales/bookyt_salary.de.yml b/config/locales/bookyt_salary.de.yml index a84a9ff..4e1d09a 100644 --- a/config/locales/bookyt_salary.de.yml +++ b/config/locales/bookyt_salary.de.yml @@ -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: @@ -37,3 +41,6 @@ de: salary_templates: index: title: Lohnvorlagen + salary_items: + list: + title: Lohnarten diff --git a/config/routes.rb b/config/routes.rb index 3aca2dc..db26cb4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20120118121223_create_salary_items.rb b/db/migrate/20120118121223_create_salary_items.rb new file mode 100644 index 0000000..66a323e --- /dev/null +++ b/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 diff --git a/spec/factories/salary_items.rb b/spec/factories/salary_items.rb new file mode 100644 index 0000000..f613506 --- /dev/null +++ b/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 diff --git a/spec/models/salary_item_spec.rb b/spec/models/salary_item_spec.rb new file mode 100644 index 0000000..b8e598b --- /dev/null +++ b/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