From 75deec9f0640d5e59dfbf6282bbc6aca2b9f61fe Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Thu, 12 Oct 2017 20:50:40 +0200 Subject: [PATCH] Add custom fields to invoice, odergroup, supplier and user --- app/models/concerns/custom_fields.rb | 16 ++++++++++++++++ app/models/invoice.rb | 1 + app/models/ordergroup.rb | 1 + app/models/supplier.rb | 3 ++- app/models/user.rb | 4 ++-- app/models/workgroup.rb | 6 +++--- app/views/admin/ordergroups/_form.html.haml | 1 + app/views/admin/workgroups/_form.html.haml | 1 + app/views/finance/invoices/_form.html.haml | 1 + app/views/shared/_custom_form_fields.html.haml | 7 +++++++ app/views/shared/_user_form_fields.html.haml | 3 ++- app/views/suppliers/_form.html.haml | 1 + config/app_config.yml.SAMPLE | 10 ++++++++++ 13 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 app/models/concerns/custom_fields.rb create mode 100644 app/views/shared/_custom_form_fields.html.haml diff --git a/app/models/concerns/custom_fields.rb b/app/models/concerns/custom_fields.rb new file mode 100644 index 00000000..04373d60 --- /dev/null +++ b/app/models/concerns/custom_fields.rb @@ -0,0 +1,16 @@ +module CustomFields + extend ActiveSupport::Concern + include RailsSettings::Extend + + attr_accessor :custom_fields + + included do + after_initialize do + settings.defaults['custom_fields'] = { } unless settings.custom_fields + end + + after_save do + self.settings.custom_fields = custom_fields if custom_fields + end + end +end diff --git a/app/models/invoice.rb b/app/models/invoice.rb index b5594e64..e4b3af26 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -1,4 +1,5 @@ class Invoice < ActiveRecord::Base + include CustomFields belongs_to :supplier belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id' diff --git a/app/models/ordergroup.rb b/app/models/ordergroup.rb index 6b74d572..a3c79a53 100644 --- a/app/models/ordergroup.rb +++ b/app/models/ordergroup.rb @@ -5,6 +5,7 @@ # Ordergroup have the following attributes, in addition to Group # * account_balance (decimal) class Ordergroup < Group + include CustomFields APPLE_MONTH_AGO = 6 # How many month back we will count tasks and orders sum diff --git a/app/models/supplier.rb b/app/models/supplier.rb index 31109d55..2f8d5b7e 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -1,6 +1,7 @@ # encoding: utf-8 class Supplier < ActiveRecord::Base include MarkAsDeletedWithName + include CustomFields has_many :articles, -> { where(:type => nil).includes(:article_category).order('article_categories.name', 'articles.name') } has_many :stock_articles, -> { includes(:article_category).order('article_categories.name', 'articles.name') } @@ -10,7 +11,7 @@ class Supplier < ActiveRecord::Base belongs_to :shared_supplier # for the sharedLists-App include ActiveModel::MassAssignmentSecurity - attr_accessible :name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number, :iban, + attr_accessible :name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number, :iban, :custom_fields, :delivery_days, :order_howto, :note, :shared_supplier_id, :min_order_quantity, :shared_sync_method validates :name, :presence => true, :length => { :in => 4..30 } diff --git a/app/models/user.rb b/app/models/user.rb index a419269c..10ab0dd8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,9 +3,9 @@ require 'digest/sha1' # specific user rights through memberships (see Group) class User < ActiveRecord::Base - include RailsSettings::Extend + include CustomFields #TODO: acts_as_paraniod ?? - + has_many :memberships, :dependent => :destroy has_many :groups, :through => :memberships #has_one :ordergroup, :through => :memberships, :source => :group, :class_name => "Ordergroup" diff --git a/app/models/workgroup.rb b/app/models/workgroup.rb index 1c7d8a59..4b5c4595 100644 --- a/app/models/workgroup.rb +++ b/app/models/workgroup.rb @@ -1,6 +1,7 @@ # encoding: utf-8 class Workgroup < Group - + include CustomFields + has_many :tasks # returns all non-finished tasks has_many :open_tasks, -> { where(:done => false).order('due_date', 'name') }, :class_name => 'Task' @@ -25,6 +26,5 @@ class Workgroup < Group errors.add(:role_admin, I18n.t('workgroups.error_last_admin_role')) end end - -end +end diff --git a/app/views/admin/ordergroups/_form.html.haml b/app/views/admin/ordergroups/_form.html.haml index 361e8087..3eb3a9f5 100644 --- a/app/views/admin/ordergroups/_form.html.haml +++ b/app/views/admin/ordergroups/_form.html.haml @@ -5,6 +5,7 @@ = f.input :contact_person = f.input :contact_phone = f.input :contact_address + = render 'shared/custom_form_fields', f: f, type: :ordergroup .fold-line = f.input :break_start, as: :date_picker, label: Ordergroup.human_attribute_name('break') = f.input :break_end, as: :date_picker, label: Ordergroup.human_attribute_name('break_until') diff --git a/app/views/admin/workgroups/_form.html.haml b/app/views/admin/workgroups/_form.html.haml index e98b2f0e..024ef5d9 100644 --- a/app/views/admin/workgroups/_form.html.haml +++ b/app/views/admin/workgroups/_form.html.haml @@ -2,6 +2,7 @@ %p= t('.first_paragraph', url: link_to(t('.here'), new_invite_path(id: @workgroup.id), remote: true)).html_safe = simple_form_for [:admin, @workgroup] do |f| - captured = capture do + = render 'shared/custom_form_fields', f: f, type: :workgroup %h4= t 'admin.access_to' = f.input :role_suppliers = f.input :role_article_meta diff --git a/app/views/finance/invoices/_form.html.haml b/app/views/finance/invoices/_form.html.haml index ca30a4d2..207c9dcb 100644 --- a/app/views/finance/invoices/_form.html.haml +++ b/app/views/finance/invoices/_form.html.haml @@ -22,6 +22,7 @@ = f.input :amount, as: :string = f.input :deposit, as: :string = f.input :deposit_credit, as: :string + = render 'shared/custom_form_fields', f: f, type: :invoice = f.input :attachment, as: :file, hint: t('.attachment_hint') - if f.object.attachment_data.present? = f.input :delete_attachment, as: :boolean diff --git a/app/views/shared/_custom_form_fields.html.haml b/app/views/shared/_custom_form_fields.html.haml new file mode 100644 index 00000000..49b4ff23 --- /dev/null +++ b/app/views/shared/_custom_form_fields.html.haml @@ -0,0 +1,7 @@ +- if FoodsoftConfig[:custom_fields] && FoodsoftConfig[:custom_fields][type] + = f.simple_fields_for :custom_fields, defaults: { required: false } do |s| + - FoodsoftConfig[:custom_fields][type].each do |options| + - options = options.deep_symbolize_keys + - name = options.delete(:name) + - value = f.object.settings.custom_fields[name] + = s.input name, options.deep_merge(input_html: {value:value}) diff --git a/app/views/shared/_user_form_fields.html.haml b/app/views/shared/_user_form_fields.html.haml index b06dc111..196b6744 100644 --- a/app/views/shared/_user_form_fields.html.haml +++ b/app/views/shared/_user_form_fields.html.haml @@ -19,10 +19,11 @@ = ogf.input :contact_address, label: t('activerecord.attributes.ordergroup.contact_address'), required: false, input_html: { title: address_hint, data: {toggle: 'tooltip', placement: 'right'} } += render 'shared/custom_form_fields', f: f, type: :user = f.simple_fields_for :settings_attributes do |s| = s.simple_fields_for :profile, defaults: { inline_label: true } do |profile| = profile.input 'language', as: :select, collection: available_locales, required: false, selected: f.object.settings.profile['language'] - + .settings .settings-group = s.simple_fields_for :profile, defaults: { inline_label: true } do |profile| diff --git a/app/views/suppliers/_form.html.haml b/app/views/suppliers/_form.html.haml index 97abefe0..02f66173 100644 --- a/app/views/suppliers/_form.html.haml +++ b/app/views/suppliers/_form.html.haml @@ -18,6 +18,7 @@ = f.input :order_howto, as: :text, input_html: {rows: 5} = f.input :note, as: :text, input_html: {rows: 5} = f.input :min_order_quantity + = render 'shared/custom_form_fields', f: f, type: :supplier - if @supplier.shared_supplier = f.input :shared_sync_method, collection: shared_sync_method_collection(@supplier.shared_supplier), input_html: {class: 'input-xlarge'}, include_blank: false, disabled: @supplier.shared_supplier.shared_sync_methods.count < 2 .form-actions diff --git a/config/app_config.yml.SAMPLE b/config/app_config.yml.SAMPLE index 501f5b81..3b5e10a4 100644 --- a/config/app_config.yml.SAMPLE +++ b/config/app_config.yml.SAMPLE @@ -108,6 +108,16 @@ default: &defaults # Custom CSS for the foodcoop #custom_css: 'body { background-color: #fcffba; }' + # Custom fields for invoice, odergroup, supplier and user. + # Check out https://github.com/plataformatec/simple_form for details about the supported options. + #custom_fields: + # user: + # - name: address + # label: Address + # - name: birthday + # label: Birthday + # as: date_picker + # Uncomment to add tracking code for web statistics, e.g. for Piwik. (Added to bottom of page) #webstats_tracking_code: | #