king_views 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +84 -0
- data/Rakefile +31 -0
- data/VERSION +1 -0
- data/init.rb +3 -0
- data/king_form/MIT-LICENSE +20 -0
- data/king_form/README.rdoc +64 -0
- data/king_form/Rakefile +23 -0
- data/king_form/init.rb +1 -0
- data/king_form/lib/king_form.rb +19 -0
- data/king_form/lib/king_form/builder/base.rb +238 -0
- data/king_form/lib/king_form/builder/definition_list.rb +119 -0
- data/king_form/lib/king_form/builder/form_fields.rb +333 -0
- data/king_form/lib/king_form/builder/form_fields_overrides.rb +146 -0
- data/king_form/lib/king_form/builder/labeled.rb +116 -0
- data/king_form/lib/king_form/helper.rb +97 -0
- data/king_form/lib/king_form/nested_form_helper.rb +61 -0
- data/king_form/lib/king_form/overrides.rb +25 -0
- data/king_form/tasks/king_forms_tasks.rake +4 -0
- data/king_form/test/king_forms_test.rb +8 -0
- data/king_form/test/test_helper.rb +3 -0
- data/king_format/MIT-LICENSE +20 -0
- data/king_format/README.rdoc +20 -0
- data/king_format/Rakefile +23 -0
- data/king_format/init.rb +1 -0
- data/king_format/lib/helpers/date_helper.rb +25 -0
- data/king_format/lib/helpers/formatting_helper.rb +108 -0
- data/king_format/lib/helpers/money_helper.rb +63 -0
- data/king_format/lib/king_format.rb +14 -0
- data/king_format/lib/model_mixins/has_date_fields.rb +42 -0
- data/king_format/lib/model_mixins/has_money_fields.rb +42 -0
- data/king_format/lib/model_mixins/has_percent_fields.rb +34 -0
- data/king_format/tasks/king_format_tasks.rake +4 -0
- data/king_format/test/king_format_test.rb +8 -0
- data/king_format/test/test_helper.rb +3 -0
- data/king_list/MIT-LICENSE +20 -0
- data/king_list/README.rdoc +21 -0
- data/king_list/Rakefile +23 -0
- data/king_list/init.rb +1 -0
- data/king_list/lib/king_list.rb +18 -0
- data/king_list/lib/king_list/app_helper.rb +30 -0
- data/king_list/lib/king_list/builder/show.rb +71 -0
- data/king_list/lib/king_list/builder/table.rb +166 -0
- data/king_list/lib/king_list/list_helper.rb +329 -0
- data/king_list/lib/king_list/overrides.rb +6 -0
- data/king_list/tasks/king_list_tasks.rake +4 -0
- data/king_list/test/king_list_test.rb +8 -0
- data/king_list/test/test_helper.rb +3 -0
- data/king_views.gemspec +85 -0
- metadata +110 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Georg Leciejewski
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
= KingViews
|
2
|
+
|
3
|
+
This is the new offical home of KingViews(forked from schorsch)
|
4
|
+
|
5
|
+
KingViews where extracted from SalesKing(https://www.SalesKing.eu) and consist
|
6
|
+
of three different view helpers, KingForm( dl || labeled forms, enhanced inputs ),
|
7
|
+
KingList( dl, tables ) and KingFormat( date, money, percent )
|
8
|
+
|
9
|
+
This stuff is not optimized(yet) to run seamlessly within any rails project.
|
10
|
+
Fixes for rails 2.3.5 and of course 3.0 are needed. Some refacturing and of
|
11
|
+
course testing(still stuck in SalesKing) should be done, leaving space for forks.
|
12
|
+
|
13
|
+
== Install
|
14
|
+
|
15
|
+
You must use HAML for your views .. the cure for erb eye cancer
|
16
|
+
|
17
|
+
gem install king_views
|
18
|
+
|
19
|
+
|
20
|
+
== KingForm
|
21
|
+
|
22
|
+
Clean up your forms with helpers in rails haml views, with support for:
|
23
|
+
* dl or labeled forms
|
24
|
+
* shorter syntax for inputs
|
25
|
+
* fieldset support (section)
|
26
|
+
* labels auto translated when present in I18n or Gettext
|
27
|
+
* info-text for auto translated info after each input
|
28
|
+
* bundle a couple of inputs under one label or dt incl. css class based counter for the wrapper
|
29
|
+
* based on rails action view helpers so all parameters are passed through(nearly all) to the original versions
|
30
|
+
|
31
|
+
=== Example
|
32
|
+
- dl_form_for @payment, :url => 'a path', :html => { :method => :post } do |f|
|
33
|
+
-f.section do
|
34
|
+
= f.hidden :lock_version
|
35
|
+
= f.text :amount, :class => 'required', :value=> 'a custom val'
|
36
|
+
= f.date :date, :title => t(:'from'), :info=>'Help me'
|
37
|
+
= f.selection :payment_method
|
38
|
+
- f.bundle 'status' do
|
39
|
+
= f.radio :new_status, :closed, :checked=>true
|
40
|
+
%span=t(:'status.closed')
|
41
|
+
= f.radio :new_status, :open
|
42
|
+
%span= t(:'activerecord.attributes.document.enum.status.open')
|
43
|
+
-f.actions do
|
44
|
+
= f.submit t(:'form.save')
|
45
|
+
= secondary_link_to t(:'link.cancel'), parent_path
|
46
|
+
|
47
|
+
== KingList
|
48
|
+
|
49
|
+
Use KingList for an easy markup inside your lists and detail views. Its not as
|
50
|
+
extensive as KingForm and only provides the following:
|
51
|
+
|
52
|
+
* easy to use tables for listings
|
53
|
+
* dl-helper for detail views
|
54
|
+
* action_icons & action_buttons(forms) for css enabled icon markup
|
55
|
+
|
56
|
+
=== Example
|
57
|
+
# Definition list for detail views
|
58
|
+
- dl_for @invoice do |f|
|
59
|
+
= f.show :number
|
60
|
+
= f.show :date # auto-formated date field
|
61
|
+
= f.show :total # auto-formated money field
|
62
|
+
|
63
|
+
# table with header-klick sorting disabled
|
64
|
+
- table_for(@payments,{:sorting => false} ) do |t, payment|
|
65
|
+
- t.action_column do
|
66
|
+
= action_button 'delete', {:url=> 'some-path', :title => t(:'link.delete'), :method => :delete, :class=>'delete'}
|
67
|
+
= t.column :date
|
68
|
+
= t.column :amount, :td_options => {opt=>val}
|
69
|
+
|
70
|
+
== KingFormat
|
71
|
+
Provides a semi-automatic formatting of date, money, percent fields. The field
|
72
|
+
types are defined inside a model. And the view helpers will show a nice output.
|
73
|
+
|
74
|
+
=== Example:
|
75
|
+
|
76
|
+
#define date fields you model definition
|
77
|
+
class Invoice < ActiveRecord::Base
|
78
|
+
has_date_fields :date, :valid_until
|
79
|
+
has_money_fields :total, :price
|
80
|
+
end
|
81
|
+
# see above for usage in views
|
82
|
+
|
83
|
+
== License
|
84
|
+
Copyright (c) 2009 Georg Leciejewski, released under the MIT-LICENSE
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'spec/rake/spectask'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |gem|
|
9
|
+
gem.name = "king_views"
|
10
|
+
gem.summary = %Q{Ultraclean haml views with list and forms helpers for rails }
|
11
|
+
gem.description = %Q{Clean up your Forms using king_form for dl or labeled forms. Use king_list for an easy markup of tables in your lists and dl-enabled listings in your detail views. }
|
12
|
+
gem.email = "gl@salesking.eu"
|
13
|
+
gem.homepage = "http://github.com/salesking/king_views"
|
14
|
+
gem.authors = ["Georg Leciejewski"]
|
15
|
+
#gem.add_development_dependency "rspec", ">= 0"
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
desc 'Generate king_views documentation.'
|
23
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
24
|
+
rdoc.rdoc_dir = 'rdoc'
|
25
|
+
rdoc.title = 'KingViews'
|
26
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
27
|
+
rdoc.rdoc_files.include('README')
|
28
|
+
rdoc.rdoc_files.include('king_form/lib/**/*.rb')
|
29
|
+
rdoc.rdoc_files.include('king_format/lib/**/*.rb')
|
30
|
+
rdoc.rdoc_files.include('king_list/lib/**/*.rb')
|
31
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/init.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,64 @@
|
|
1
|
+
= KingForm
|
2
|
+
|
3
|
+
Writing forms is is an enerving repetitive task. It already got easier through rails
|
4
|
+
but still you have to huddle with a lot of html, erb markup and options around your fields.
|
5
|
+
|
6
|
+
King forms internals are based on haml, rails' form helpers, I18n translations
|
7
|
+
and a simplified syntax for fieldnames.
|
8
|
+
|
9
|
+
Additionally it includes a nested_form_helper to easiely show related models in a form.
|
10
|
+
|
11
|
+
== Example KingForm
|
12
|
+
ruby code
|
13
|
+
- dl_form_for( current_object, :url => object_url, :html => { :id => 'productForm'} ) do |f|
|
14
|
+
- f.section 'Name & Number' do
|
15
|
+
= f.hidden :lock_version
|
16
|
+
= f.text :name, :class => 'required'
|
17
|
+
= f.text :number
|
18
|
+
= f.selection :category
|
19
|
+
= f.selection :quantity_unit, :choices => ['some', 'choices']
|
20
|
+
- f.section '' do
|
21
|
+
= f.selection :tax, :choices => ['tax1', 'tax2'], :include_blank => false
|
22
|
+
= f.text :price, :class => 'required'
|
23
|
+
- f.actions do
|
24
|
+
= f.submit t(:'form.save')
|
25
|
+
= link_to t(:'link.cancel'), object_path
|
26
|
+
|
27
|
+
== Example Nested Forms
|
28
|
+
|
29
|
+
The first thing you need to do is enable attributes on the association in the model.
|
30
|
+
ruby code
|
31
|
+
class Project < ActiveRecord::Base
|
32
|
+
has_many :tasks
|
33
|
+
accepts_nested_attributes_for :tasks
|
34
|
+
end
|
35
|
+
|
36
|
+
The partial for the nested object(one Task) should have the name of the associated element's type( or the partial to use)
|
37
|
+
|
38
|
+
## projects/_task.html.erb
|
39
|
+
<div class="task">
|
40
|
+
<label>Title</label>
|
41
|
+
<%= f.text_field :title %>
|
42
|
+
</div>
|
43
|
+
|
44
|
+
In the parent element's form (Project), call the render_nested_form method, with the collection of
|
45
|
+
elements you'd like to render as the first argument.
|
46
|
+
|
47
|
+
## projects/_form.html.erb
|
48
|
+
<%= f.render_nested_form(@project.tasks) %>
|
49
|
+
|
50
|
+
That call will render the partial named _task.html.erb with each element in the supplied collection of tasks, wrapping
|
51
|
+
the partial in a form builder (fields_for) with all the necessary arguments to produce a hash that will satisfy the
|
52
|
+
tasks_attributes method(provided by Rails new accepts_nested_attributes_for).
|
53
|
+
|
54
|
+
=== Options
|
55
|
+
See comments in lib/nested_form_helper for full reference of options.
|
56
|
+
You may want to add a few blank tasks to the bottom of your form; no need to do that in the controller anymore.
|
57
|
+
|
58
|
+
<%= f.render_nested_form(@project.tasks, :new => 3, :partial=>'some_partial', :locals=>..) %>
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
This plugin was extracted from SalesKing where it was developed by Georg Ledermann
|
63
|
+
and Georg Leciejewski
|
64
|
+
Copyright (c) 2009 Georg Leciejewski, released under the MIT license
|
data/king_form/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the king_forms plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the king_forms plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'King-forms'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
data/king_form/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/lib/king_form"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/king_form/helper"
|
2
|
+
require "#{File.dirname(__FILE__)}/king_form/overrides"
|
3
|
+
require "#{File.dirname(__FILE__)}/king_form/builder/form_fields_overrides"
|
4
|
+
require "#{File.dirname(__FILE__)}/king_form/builder/form_fields"
|
5
|
+
require "#{File.dirname(__FILE__)}/king_form/builder/base"
|
6
|
+
require "#{File.dirname(__FILE__)}/king_form/builder/definition_list"
|
7
|
+
require "#{File.dirname(__FILE__)}/king_form/builder/labeled"
|
8
|
+
require "#{File.dirname(__FILE__)}/king_form/nested_form_helper"
|
9
|
+
|
10
|
+
# You need:
|
11
|
+
# - haml
|
12
|
+
# - mutlilang via I18n or gettext ??
|
13
|
+
# -switch to allow default rails helper
|
14
|
+
|
15
|
+
# extend AC with our helper module
|
16
|
+
ActionController::Base.helper KingForm::Helper
|
17
|
+
|
18
|
+
# include the nested_form helper method on the very top of FormBuilder
|
19
|
+
ActionView::Helpers::FormBuilder.class_eval { include KingForm::NestedFormHelper }
|
@@ -0,0 +1,238 @@
|
|
1
|
+
module KingForm
|
2
|
+
module Builder
|
3
|
+
# Base Form Helper Class overrides most of rails build in form / field methods.
|
4
|
+
# This class is used in conjunction with haml and makes your templates ultra clean.
|
5
|
+
#
|
6
|
+
# To further specify the resulting html for form fields and their wrappers,
|
7
|
+
# subclass KingForm::Builder::Base.
|
8
|
+
#
|
9
|
+
# Subclasses must define the following methods:
|
10
|
+
#
|
11
|
+
# ==== def section
|
12
|
+
# Definition of a section inside a form. A section is a wrapper around fields
|
13
|
+
# which belong together. The topmost element should be(most of the time) a fieldset
|
14
|
+
# followed by a legend and the openeing definition list.
|
15
|
+
# fieldset-> legend -> div -> ....
|
16
|
+
# fieldset-> legend -> dl -> ...
|
17
|
+
# See child class Labeled and DefinitionList for examples.
|
18
|
+
#
|
19
|
+
# ==== def tag_wrapper
|
20
|
+
# Wrapping/display of field descriptions(labels) and input tags
|
21
|
+
# => wrapped with div. actually no wrapping(see above):
|
22
|
+
# fieldset-> div -> label -> input
|
23
|
+
# => wrapped with dt / dd
|
24
|
+
# fieldset-> dl -> dd -> dt -> input
|
25
|
+
#
|
26
|
+
#
|
27
|
+
# ==== def bundle
|
28
|
+
# handle fields which are displayed toghether inside a wrapper
|
29
|
+
class Base < ActionView::Helpers::FormBuilder
|
30
|
+
include KingForm::Builder::FormFields
|
31
|
+
include KingForm::Builder::FormFieldsOverrides
|
32
|
+
attr_accessor :no_wrap
|
33
|
+
def initialize(object_name, object, template, options, proc)
|
34
|
+
# Store builder configuration data (only used by "render_associated_form")
|
35
|
+
@config = options.delete(:config) || {}
|
36
|
+
|
37
|
+
super(object_name, object, template, options, proc)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Create a section(fieldset) within a form
|
41
|
+
# A section is a group of related object information with name/value pairs,
|
42
|
+
# like all dates of an object or the users name fields(last/first/title/nick).
|
43
|
+
def section(title = nil, options = {}, &block)
|
44
|
+
#must be overwritten in inerit classes
|
45
|
+
end
|
46
|
+
|
47
|
+
# Show multiple inputs as bundle
|
48
|
+
def bundle(title = nil, options = {}, &block)
|
49
|
+
#must be redefined in inerit class
|
50
|
+
end
|
51
|
+
|
52
|
+
# For using with nested forms with associated records => contact => addresses
|
53
|
+
# Another Builder instance will be created. Make sure it's also a DefinitionsListsFormBuilder
|
54
|
+
# and configuration data is available for the new created instance
|
55
|
+
def render_nested_form(associated, opts = {})
|
56
|
+
opts[:fields_for] = { :builder => KingForm::Builder::DefinitionList, :config => @config }
|
57
|
+
super(associated, opts)
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
# wraps a list of action links/buttons in a td od div
|
62
|
+
# those wrappers can then be formated with css td.actions
|
63
|
+
# also see #ListHelper
|
64
|
+
#
|
65
|
+
# ===Example haml
|
66
|
+
# = f.actions do
|
67
|
+
# = link_to my_actions
|
68
|
+
#
|
69
|
+
# <div class="actions">my actions</div>
|
70
|
+
# <td class="actions">my actions</td>
|
71
|
+
#
|
72
|
+
def actions(options = {}, &block)
|
73
|
+
options[:class] ||= 'actions'
|
74
|
+
if @config[:table]
|
75
|
+
@template.haml_tag :td, @template.capture_haml(&block), options
|
76
|
+
else
|
77
|
+
@template.haml_tag :div, @template.capture_haml(&block), options
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
#build a table
|
82
|
+
def table(title, options = {}, &block)
|
83
|
+
# Capture the block (with analyzing the header titles)
|
84
|
+
@config[:table] = true
|
85
|
+
@config[:column_header] = []
|
86
|
+
@config[:row_number] = 0
|
87
|
+
content = @template.capture_haml(&block)
|
88
|
+
|
89
|
+
# Now build the whole table tag
|
90
|
+
result = @template.capture_haml do
|
91
|
+
@template.haml_tag :table, options.reverse_merge({ :summary => title }) do
|
92
|
+
@template.haml_tag :thead do
|
93
|
+
@template.haml_tag :tr do
|
94
|
+
@config[:column_header].each do |c|
|
95
|
+
c[:options][:class] ||= ''
|
96
|
+
if c == @config[:column_header].first
|
97
|
+
c[:options][:class] << ' first'
|
98
|
+
elsif c == @config[:column_header].last
|
99
|
+
c[:options][:class] << ' last'
|
100
|
+
end
|
101
|
+
|
102
|
+
@template.haml_tag :th, c[:title], c[:options]
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
@template.haml_tag :tbody, content
|
107
|
+
end
|
108
|
+
end
|
109
|
+
@config[:table] = false
|
110
|
+
|
111
|
+
return result
|
112
|
+
end
|
113
|
+
|
114
|
+
# Build a single table row, only needed to be be able to render the table
|
115
|
+
# headers(th)
|
116
|
+
def table_row(&block)
|
117
|
+
@config[:row_number] += 1
|
118
|
+
@template.concat "<tr> #{@template.capture(&block)}</tr>"
|
119
|
+
end
|
120
|
+
|
121
|
+
private
|
122
|
+
# returns the current object
|
123
|
+
def current_object
|
124
|
+
# check for fields made with attribute_fu
|
125
|
+
if @object_name.to_s.match(/\[\w+_attributes\]/)
|
126
|
+
#field was constructed via attribute_fu: user[address_attributes][atLE1aPLKr3j9zabTJhScS]
|
127
|
+
@object
|
128
|
+
else
|
129
|
+
# Instance-Variable of the templates
|
130
|
+
@template.instance_variable_get("@#{@object_name}")
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# returns the class of the current object
|
135
|
+
def current_class
|
136
|
+
current_object.class
|
137
|
+
end
|
138
|
+
|
139
|
+
# returns the value of an attribute belonging to the current object
|
140
|
+
def current_value(fieldname)
|
141
|
+
if current_object.is_a?(Hash)
|
142
|
+
current_object[fieldname]
|
143
|
+
else
|
144
|
+
current_object.send(fieldname) rescue nil
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
|
149
|
+
# Shortcut for using "content_tag", which exists in the context of the template
|
150
|
+
def content_tag(*args)
|
151
|
+
@template.content_tag(*args)
|
152
|
+
end
|
153
|
+
|
154
|
+
# Translate acts_as_enum dropdown field values either with I18n
|
155
|
+
# A key must be lokated in the language file under:
|
156
|
+
# "activerecord.attributes.client.enum.sending_methods.fax"
|
157
|
+
# "activerecord.attributes.client.enum.sending_methods.email"
|
158
|
+
# ==== Parameter
|
159
|
+
# fieldname<String,Symbol>:: The fieldname in the model which holds enum values from acts_as_enum plugin
|
160
|
+
# === Returns
|
161
|
+
# <Hash{'translated fldname'=>'value'}>::
|
162
|
+
def enum_values(fieldname)
|
163
|
+
# Check if there is a const in the class defined by acts_as_enum
|
164
|
+
return unless current_class.const_defined?(fieldname.to_s.upcase)
|
165
|
+
# Get Array with the values from this constant
|
166
|
+
values = current_class.const_get(fieldname.to_s.upcase)
|
167
|
+
# values are symbols as defined by "acts_as_enum"
|
168
|
+
if values && values.first.is_a?(Symbol)
|
169
|
+
values_with_translated_keys = {}
|
170
|
+
values.each do |value|
|
171
|
+
key = current_class.human_attribute_name("enum.#{fieldname.to_s}.#{value.to_s}")
|
172
|
+
values_with_translated_keys[key] = value.to_s
|
173
|
+
end
|
174
|
+
return values_with_translated_keys
|
175
|
+
else
|
176
|
+
#values are not symbols (probably not coming from acts_as_enum) =>return them unchanged
|
177
|
+
return values
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
# add titles to Input-Tag and embed/wrap in dt/dd
|
182
|
+
# options: Hash with following keys
|
183
|
+
# :dt => options for dt
|
184
|
+
# :dd => options for dd
|
185
|
+
def tag_wrapper(fieldname_or_title, tags, options = {})
|
186
|
+
#overwrite in inherit class !!!
|
187
|
+
end
|
188
|
+
|
189
|
+
# Build a fields title/label with translation
|
190
|
+
# takes the class and fieldname (like GetText ActiveRecord-Parser )
|
191
|
+
# ==== Parameter
|
192
|
+
# fieldname_or_title<String,Symbol>:: A string is directly returned.
|
193
|
+
# A Symbol is beeing looked up in I18n translation inside the models attribute namespace:
|
194
|
+
# => class.human_attribute_name(fieldname_or_title.to_s)
|
195
|
+
def build_title(fieldname_or_title)
|
196
|
+
if fieldname_or_title.is_a?(Symbol)
|
197
|
+
#i18n namespace under activerecord.attributes.model.attr_name
|
198
|
+
current_class.human_attribute_name(fieldname_or_title.to_s) if current_class.respond_to?(:human_attribute_name)
|
199
|
+
else
|
200
|
+
fieldname_or_title
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# Build span-tag with an info text after a field
|
205
|
+
# ==== Parameter
|
206
|
+
# fieldname_or_text<String/Symbol>:: static text value or a fieldname as symbol.
|
207
|
+
# If a symbol is given the translated text is taken from I18n translation file
|
208
|
+
def info_tag(fieldname_or_text)
|
209
|
+
case fieldname_or_text
|
210
|
+
when String #just use the plain string
|
211
|
+
value = fieldname_or_text
|
212
|
+
when Symbol # lookup the the field in i18n under activerecord.attributes.class.fieldname_info
|
213
|
+
trans = I18n.translate("#{current_class.name.underscore}.#{fieldname_or_text.to_s}_info",
|
214
|
+
:default => '',
|
215
|
+
:scope => [:activerecord, :attributes])
|
216
|
+
value = trans.blank? ? nil : trans
|
217
|
+
else
|
218
|
+
raise ArgumentError
|
219
|
+
end
|
220
|
+
|
221
|
+
value ? content_tag(:div, value, :class => 'info') : ''
|
222
|
+
end
|
223
|
+
|
224
|
+
|
225
|
+
# Create the id of a field
|
226
|
+
# ==== Parameter
|
227
|
+
# name<String>::The name of the id
|
228
|
+
def build_id(name)
|
229
|
+
if current_object.blank?
|
230
|
+
name
|
231
|
+
else
|
232
|
+
"#{@object_name}_#{name}"
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
end #Class Base
|
237
|
+
end#Module Builder
|
238
|
+
end#Module KingForm
|