king_views 1.1.6 → 1.2.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.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/Gemfile +19 -0
- data/Rakefile +5 -28
- data/king_form/lib/king_form/builder/base.rb +23 -9
- data/king_form/lib/king_form/builder/definition_list.rb +8 -4
- data/king_form/lib/king_form/builder/form_fields.rb +3 -2
- data/king_form/lib/king_form/builder/form_fields_overrides.rb +5 -5
- data/king_form/lib/king_form/builder/labeled.rb +3 -3
- data/king_form/lib/king_form/helper.rb +21 -4
- data/king_form/lib/king_form/nested_form_helper.rb +9 -9
- data/king_form/lib/king_form/overrides.rb +13 -13
- data/king_format/lib/helpers/formatting_helper.rb +21 -16
- data/king_format/lib/helpers/money_helper.rb +6 -6
- data/king_format/lib/king_format.rb +2 -0
- data/king_format/lib/model_mixins/has_date_fields.rb +6 -2
- data/king_format/lib/model_mixins/has_money_fields.rb +6 -2
- data/king_format/lib/model_mixins/has_percent_fields.rb +5 -1
- data/king_list/Rakefile +2 -13
- data/king_list/lib/king_list/builder/show.rb +9 -6
- data/king_list/lib/king_list/builder/table.rb +29 -72
- data/king_list/lib/king_list/list_helper.rb +60 -63
- data/king_views.gemspec +24 -71
- data/spec/king_list_spec.rb +19 -0
- data/spec/rails_app/Rakefile +7 -0
- data/spec/rails_app/app/controllers/application_controller.rb +4 -0
- data/spec/rails_app/app/controllers/posts_controller.rb +6 -0
- data/spec/rails_app/app/helpers/application_helper.rb +2 -0
- data/spec/rails_app/app/models/comment.rb +3 -0
- data/spec/rails_app/app/models/person.rb +3 -0
- data/spec/rails_app/app/models/post.rb +4 -0
- data/spec/rails_app/app/views/layouts/application.html.erb +14 -0
- data/spec/rails_app/app/views/posts/index.haml +23 -0
- data/spec/rails_app/config.ru +4 -0
- data/spec/rails_app/config/application.rb +44 -0
- data/spec/rails_app/config/boot.rb +10 -0
- data/spec/rails_app/config/database.yml +22 -0
- data/spec/rails_app/config/environment.rb +5 -0
- data/spec/rails_app/config/environments/development.rb +26 -0
- data/spec/rails_app/config/environments/production.rb +49 -0
- data/spec/rails_app/config/environments/test.rb +35 -0
- data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/rails_app/config/initializers/inflections.rb +10 -0
- data/spec/rails_app/config/initializers/mime_types.rb +5 -0
- data/spec/rails_app/config/initializers/secret_token.rb +7 -0
- data/spec/rails_app/config/initializers/session_store.rb +8 -0
- data/spec/rails_app/config/locales/en.yml +5 -0
- data/spec/rails_app/config/routes.rb +60 -0
- data/spec/rails_app/db/migrate/20110306212208_create_posts.rb +14 -0
- data/spec/rails_app/db/migrate/20110306212250_create_comments.rb +15 -0
- data/spec/rails_app/db/migrate/20110420222224_create_people.rb +15 -0
- data/spec/rails_app/db/schema.rb +30 -0
- data/spec/rails_app/public/404.html +26 -0
- data/spec/rails_app/public/422.html +26 -0
- data/spec/rails_app/public/500.html +26 -0
- data/spec/rails_app/public/favicon.ico +0 -0
- data/spec/rails_app/script/rails +6 -0
- data/spec/spec_helper.rb +65 -0
- metadata +238 -47
- data/king_list/test/king_list_test.rb +0 -9
- data/king_list/test/test_helper.rb +0 -3
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7528c9f76a2b366d8a4f42001c70b7a8566cf4b2
|
4
|
+
data.tar.gz: cf91b3cbe24369196457f2e92b53983437ea538d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e8de0b59e7dabc1aef47faec45155040c9ce46f8f9e44b301a4423a3e31582ebc866b5b8906d8e3b50909800aeadb3ebd42923c41e3773a411f9468568279d31
|
7
|
+
data.tar.gz: f601ef0eb413ce869aed79f58fa77a3fa7f70aedc7ea438886be734dd81f26387ae95913802e12e4b024dc2ca2101c5b36a642535afd8169860cf2543b00a255
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
gemspec
|
3
|
+
|
4
|
+
#
|
5
|
+
# group :development, :test do
|
6
|
+
# gem 'rails', '>= 3.0.0'
|
7
|
+
# gem "haml"
|
8
|
+
# gem "sqlite3-ruby", :require => "sqlite3"
|
9
|
+
# gem 'jeweler', '>= 1.5.2'
|
10
|
+
# gem 'rake'
|
11
|
+
# gem 'tzinfo'
|
12
|
+
# gem 'rdoc'
|
13
|
+
# gem 'rspec'
|
14
|
+
# gem "rspec-rails"
|
15
|
+
# # For debugging under ruby 1.9 special gems are needed
|
16
|
+
# # gem 'ruby-debug19', :platform => :mri
|
17
|
+
# # gem 'ruby-debug-base19', '>=0.11.26'
|
18
|
+
# # gem 'linecache19', '>=0.5.13'
|
19
|
+
# end
|
data/Rakefile
CHANGED
@@ -1,31 +1,8 @@
|
|
1
|
-
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require 'bundler/gem_tasks'
|
2
3
|
require 'rake'
|
3
|
-
require 'rdoc/task'
|
4
4
|
require 'rspec/core/rake_task'
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
6
|
+
RSpec::Core::RakeTask.new('spec')
|
7
|
+
desc 'Default: run specs.'
|
8
|
+
task :default => :spec
|
@@ -57,7 +57,7 @@ module KingForm
|
|
57
57
|
super(associated, opts)
|
58
58
|
end
|
59
59
|
|
60
|
-
|
60
|
+
|
61
61
|
# wraps a list of action links/buttons in a td od div
|
62
62
|
# those wrappers can then be formated with css td.actions
|
63
63
|
# also see #ListHelper
|
@@ -112,7 +112,7 @@ module KingForm
|
|
112
112
|
end
|
113
113
|
|
114
114
|
# Build a single table row, only needed to be be able to render the table
|
115
|
-
# headers(th)
|
115
|
+
# headers(th)
|
116
116
|
def table_row(&block)
|
117
117
|
@config[:row_number] += 1
|
118
118
|
@template.concat "<tr> #{@template.capture(&block)}</tr>"
|
@@ -168,7 +168,7 @@ module KingForm
|
|
168
168
|
if values && values.first.is_a?(Symbol)
|
169
169
|
values_with_translated_keys = {}
|
170
170
|
values.each do |value|
|
171
|
-
key = current_class
|
171
|
+
key = translate_namespaced_enum_key(current_class, fieldname, value)
|
172
172
|
values_with_translated_keys[key] = value.to_s
|
173
173
|
end
|
174
174
|
return values_with_translated_keys
|
@@ -178,6 +178,17 @@ module KingForm
|
|
178
178
|
end
|
179
179
|
end
|
180
180
|
|
181
|
+
# translation_key for fieldname
|
182
|
+
# replaces current_class.human_attribute_name("enum.#{fieldname.to_s}.#{value.to_s}")
|
183
|
+
# as `human_attribute_name` changed the way how "."'s are interpreted in 3.2.x (now as namespaces)
|
184
|
+
def translate_namespaced_enum_key(klass, field, value)
|
185
|
+
defaults = klass.lookup_ancestors.map do |_klass|
|
186
|
+
:"#{klass.i18n_scope}.attributes.#{_klass.model_name.i18n_key}.enum.#{field.to_s}.#{value.to_s}"
|
187
|
+
end
|
188
|
+
|
189
|
+
I18n.translate(default: defaults)
|
190
|
+
end
|
191
|
+
|
181
192
|
# add titles to Input-Tag and embed/wrap in dt/dd
|
182
193
|
# options: Hash with following keys
|
183
194
|
# :dt => options for dt
|
@@ -199,7 +210,7 @@ module KingForm
|
|
199
210
|
else
|
200
211
|
fieldname_or_title
|
201
212
|
end
|
202
|
-
end
|
213
|
+
end
|
203
214
|
|
204
215
|
# Build span-tag with an info text after a field
|
205
216
|
# ==== Parameter
|
@@ -210,18 +221,21 @@ module KingForm
|
|
210
221
|
when String #just use the plain string
|
211
222
|
value = fieldname_or_text
|
212
223
|
when Symbol # lookup the the field in i18n under activerecord.attributes.class.fieldname_info
|
213
|
-
|
214
|
-
|
224
|
+
key = "#{current_class.name.underscore}.#{fieldname_or_text.to_s}_info"
|
225
|
+
trans = I18n.translate("#{key}_html",
|
226
|
+
:default => I18n.translate(key,
|
227
|
+
:default => '',
|
228
|
+
:scope => [:activerecord, :attributes]),
|
215
229
|
:scope => [:activerecord, :attributes])
|
216
230
|
value = trans.blank? ? nil : trans
|
217
231
|
else
|
218
232
|
raise ArgumentError
|
219
233
|
end
|
220
234
|
|
221
|
-
value ? content_tag(:div, value, :class => 'info') : ''
|
235
|
+
value ? content_tag(:div, value.html_safe, :class => 'info').html_safe : ''
|
222
236
|
end
|
223
237
|
|
224
|
-
|
238
|
+
|
225
239
|
# Create the id of a field
|
226
240
|
# ==== Parameter
|
227
241
|
# name<String>::The name of the id
|
@@ -235,4 +249,4 @@ module KingForm
|
|
235
249
|
|
236
250
|
end #Class Base
|
237
251
|
end#Module Builder
|
238
|
-
end#Module KingForm
|
252
|
+
end#Module KingForm
|
@@ -66,7 +66,7 @@ module KingForm
|
|
66
66
|
#
|
67
67
|
# ==== Parameter
|
68
68
|
# fieldname_or_title<Symbol, String>:: The fieldname to be used as the title in dt
|
69
|
-
# tags<String>:: a bunch of html/haml tags
|
69
|
+
# tags<String>:: a bunch of html/haml tags
|
70
70
|
# options<Hash{Symbol=>String}>: Hash with following keys
|
71
71
|
# ==== Options:
|
72
72
|
# :dt => options hash for dt
|
@@ -85,7 +85,7 @@ module KingForm
|
|
85
85
|
@template.haml_tag(:td, tags.to_s, options)
|
86
86
|
end
|
87
87
|
else #wrap or no wrap
|
88
|
-
@no_wrap ? tags : dt_tag(fieldname_or_title, options[:dt]) + dd_tag(tags.to_s, options[:dd])
|
88
|
+
(@no_wrap ? tags : dt_tag(fieldname_or_title, options[:dt]) + dd_tag(tags.to_s, options[:dd])).html_safe
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
@@ -100,7 +100,11 @@ module KingForm
|
|
100
100
|
@bundle_counter = 0
|
101
101
|
tags = @template.capture(&block)
|
102
102
|
@config[:bundle] = false
|
103
|
-
|
103
|
+
if @config[:table]
|
104
|
+
tag_wrapper(title, tags)
|
105
|
+
else
|
106
|
+
tag_wrapper(title, tags, :dt => options, :dd => { :class => "elements_#{@bundle_counter}" })
|
107
|
+
end
|
104
108
|
end
|
105
109
|
|
106
110
|
# The definition type dt tag
|
@@ -116,4 +120,4 @@ module KingForm
|
|
116
120
|
|
117
121
|
end
|
118
122
|
end
|
119
|
-
end
|
123
|
+
end
|
@@ -154,6 +154,7 @@ module KingForm
|
|
154
154
|
options[:include_blank] = true unless options.has_key?(:include_blank)
|
155
155
|
options[:selected] ||= value.is_a?(Array) ? value : value.to_s
|
156
156
|
|
157
|
+
html_options[:'data-placeholder'] ||= title
|
157
158
|
# Got an AR object so full automatic contruction should work
|
158
159
|
if current_object.is_a?(ActiveRecord::Base) && fieldname.is_a?(Symbol)
|
159
160
|
# try to sort by key f.ex. when transl. enum_fields
|
@@ -219,7 +220,7 @@ module KingForm
|
|
219
220
|
select_options << "</optgroup>"
|
220
221
|
end
|
221
222
|
end
|
222
|
-
tag_wrapper title, select_tag(fieldname, select_options, options)
|
223
|
+
tag_wrapper title, select_tag(fieldname, select_options.html_safe, options)
|
223
224
|
end
|
224
225
|
|
225
226
|
def time_zone_selection(fieldname, options = {}, html_options = {})
|
@@ -281,4 +282,4 @@ module KingForm
|
|
281
282
|
|
282
283
|
end
|
283
284
|
end
|
284
|
-
end
|
285
|
+
end
|
@@ -16,9 +16,9 @@ module KingForm
|
|
16
16
|
super(method, opts) + info_tag(info_text || method)
|
17
17
|
end
|
18
18
|
|
19
|
-
def check_box(method, opts = {})
|
19
|
+
def check_box(method, opts = {}, checked_value = "1", unchecked_value = "0")
|
20
20
|
info_text = opts.delete(:info)
|
21
|
-
super(method, opts) + info_tag(info_text || method)
|
21
|
+
super(method, opts, checked_value, unchecked_value) + info_tag(info_text || method)
|
22
22
|
end
|
23
23
|
|
24
24
|
def radio_button(method, tag_value, opts = {})
|
@@ -53,7 +53,7 @@ module KingForm
|
|
53
53
|
name, infos, opts = build_id_name_info(name, opts)
|
54
54
|
@template.text_field_tag(name, value, opts) + infos
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
# Create a hidden field tag and construct its fieldname (object[name]) from
|
58
58
|
# the current object
|
59
59
|
# When the name is beeing passed in as string its just taken like it is
|
@@ -132,7 +132,7 @@ module KingForm
|
|
132
132
|
# build info tag, cause info_tag(:symbol) is looking into I18n transl
|
133
133
|
infos = info_tag( opts.delete(:info) || name)
|
134
134
|
#now set real name as string
|
135
|
-
name = "#{@object_name}[#{name}]"
|
135
|
+
name = "#{@object_name}[#{name}]".html_safe
|
136
136
|
else
|
137
137
|
opts[:id] ||= nil
|
138
138
|
info_text = opts.delete(:info)
|
@@ -143,4 +143,4 @@ module KingForm
|
|
143
143
|
|
144
144
|
end
|
145
145
|
end
|
146
|
-
end
|
146
|
+
end
|
@@ -70,7 +70,7 @@ module KingForm
|
|
70
70
|
@bundle_counter = 0
|
71
71
|
tags = @template.capture(&block)
|
72
72
|
@config[:bundle] = false
|
73
|
-
|
73
|
+
tag_wrapper(title, tags.html_safe, options)
|
74
74
|
end
|
75
75
|
|
76
76
|
# Add titles/labels to input tag and wrap in div
|
@@ -86,7 +86,7 @@ module KingForm
|
|
86
86
|
def tag_wrapper(fieldname_or_title, tags, options = {})
|
87
87
|
if @config[:bundle]
|
88
88
|
@bundle_counter += 1
|
89
|
-
tags
|
89
|
+
tags.html_safe
|
90
90
|
elsif @config[:table] # called from "table" => build a table cell (td)
|
91
91
|
# Only in first row: Build column header
|
92
92
|
if @config[:row_number] == 1
|
@@ -102,7 +102,7 @@ module KingForm
|
|
102
102
|
else # other tags stay outside label tag, because they don't like to be wrapped sometimes
|
103
103
|
label_tag(fieldname_or_title, options[:label]) + tags
|
104
104
|
end
|
105
|
-
"<div> #{out}</div>"
|
105
|
+
"<div> #{out}</div>".html_safe
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
@@ -35,7 +35,17 @@ module KingForm
|
|
35
35
|
def dl_fields_for(record_or_name_or_array, *args, &block)
|
36
36
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
37
37
|
options[:builder] = KingForm::Builder::DefinitionList
|
38
|
-
|
38
|
+
|
39
|
+
case record_or_name_or_array
|
40
|
+
when String, Symbol
|
41
|
+
object_name = record_or_name_or_array
|
42
|
+
object = args.first
|
43
|
+
else
|
44
|
+
object = record_or_name_or_array
|
45
|
+
object_name = ActiveModel::Naming.singular(object)
|
46
|
+
end
|
47
|
+
|
48
|
+
fields_for(object_name, object, options, &block)
|
39
49
|
end
|
40
50
|
|
41
51
|
# renders a form with the KingForm::Builder::Labeled
|
@@ -68,8 +78,15 @@ module KingForm
|
|
68
78
|
#
|
69
79
|
def labeled_form_for(record_or_name_or_array, *args, &proc)
|
70
80
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
71
|
-
options[:builder] =
|
72
|
-
|
81
|
+
options[:builder] = KingForm::Builder::Labeled
|
82
|
+
|
83
|
+
case record_or_name_or_array
|
84
|
+
when String, Symbol
|
85
|
+
options[:as] = record_or_name_or_array
|
86
|
+
form_for(args.shift, *(args << options), &proc)
|
87
|
+
else
|
88
|
+
form_for(record_or_name_or_array, *(args << options), &proc)
|
89
|
+
end
|
73
90
|
end
|
74
91
|
|
75
92
|
def labeled_fields_for(record_or_name_or_array, *args, &block)
|
@@ -94,4 +111,4 @@ module KingForm
|
|
94
111
|
result
|
95
112
|
end
|
96
113
|
end
|
97
|
-
end
|
114
|
+
end
|
@@ -2,15 +2,15 @@ module KingForm
|
|
2
2
|
# was attribute_fu with changes from: http://github.com/odadata/attribute_fu/commit/a0402d12f2d380d8decc82bdfbcc5d8a0a185524
|
3
3
|
# Methods for building forms that contain fields for associated models.
|
4
4
|
module NestedFormHelper
|
5
|
-
|
5
|
+
|
6
6
|
# Renders the form for nested objects defined via activerecord accepts_nested_attributes_for
|
7
7
|
#
|
8
8
|
# The associated argument can be either an object, or a collection of objects to be rendered.
|
9
9
|
#
|
10
10
|
# An options hash can be specified to override the default behaviors.
|
11
|
-
#
|
11
|
+
#
|
12
12
|
# Options are:
|
13
|
-
# * <tt>:new</tt> - specify a certain number of new elements to be added to the form. Useful for displaying a
|
13
|
+
# * <tt>:new</tt> - specify a certain number of new elements to be added to the form. Useful for displaying a
|
14
14
|
# few blank elements at the bottom.
|
15
15
|
# * <tt>:name</tt> - override the name of the association, both for the field names, and the name of the partial
|
16
16
|
# * <tt>:partial</tt> - specify the name of the partial in which the form is located.
|
@@ -21,8 +21,8 @@ module KingForm
|
|
21
21
|
# eg. obj.addresses, render the firt address on top of form, render all the other addresses at the bottom
|
22
22
|
#
|
23
23
|
def render_nested_form(associated, opts = {})
|
24
|
-
associated = associated.is_a?(Array) ? associated : [associated] # preserve association proxy if this is one
|
25
|
-
opts.symbolize_keys
|
24
|
+
associated = associated.is_a?(Array) ? associated : [associated] # preserve association proxy if this is one
|
25
|
+
opts = opts.to_hash.symbolize_keys
|
26
26
|
(opts[:new] - associated.select(&:new_record?).length).times { associated.build } if opts[:new]
|
27
27
|
|
28
28
|
unless associated.empty?
|
@@ -43,13 +43,13 @@ module KingForm
|
|
43
43
|
#The current objects classname is always present in partial so:
|
44
44
|
#when Object is LineItem locals has :line_item => object
|
45
45
|
:locals => {name.to_sym => f.object, :f => f}.merge( opts[:locals] || {} )
|
46
|
-
}.merge( opts[:render] || {} ) )
|
46
|
+
}.merge( opts[:render] || {} ) )
|
47
47
|
end
|
48
48
|
end
|
49
|
-
output.join
|
49
|
+
output.join.html_safe
|
50
50
|
end
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
private
|
54
54
|
def association_name(class_name)
|
55
55
|
@object.respond_to?("#{class_name}_attributes=") ? class_name : class_name.pluralize
|
@@ -58,4 +58,4 @@ module KingForm
|
|
58
58
|
(hash.delete(option) || object.class.name.split('::').last.underscore).to_s
|
59
59
|
end
|
60
60
|
end
|
61
|
-
end
|
61
|
+
end
|
@@ -9,17 +9,17 @@ module ActionView::Helpers::FormTagHelper
|
|
9
9
|
private
|
10
10
|
# overridden method to kill inline styles
|
11
11
|
# from actionpack-2.1.0/lib/action_view/helpers/form_tag_helper.rb
|
12
|
-
def extra_tags_for_form(html_options)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
12
|
+
#def extra_tags_for_form(html_options)
|
13
|
+
# case method = html_options.delete("method").to_s
|
14
|
+
# when /^get$/i # must be case-insentive, but can't use downcase as might be nil
|
15
|
+
# html_options["method"] = "get"
|
16
|
+
# ''
|
17
|
+
# when /^post$/i, "", nil
|
18
|
+
# html_options["method"] = "post"
|
19
|
+
# protect_against_forgery? ? content_tag(:div, token_tag) : ''
|
20
|
+
# else
|
21
|
+
# html_options["method"] = "post"
|
22
|
+
# content_tag(:div, tag(:input, :type => "hidden", :name => "_method", :value => method) + token_tag)
|
23
|
+
# end
|
24
|
+
#end
|
25
25
|
end
|
@@ -16,7 +16,7 @@ module KingFormat
|
|
16
16
|
# When set this will be used instead of asking the object for its value.
|
17
17
|
# See types for more
|
18
18
|
# opts<Hash{Symbol=>String}>:: Options
|
19
|
-
#
|
19
|
+
#
|
20
20
|
# ==== Options opts
|
21
21
|
# :currency<Hash{Symbol=>String}>:: Currency settings to format string as
|
22
22
|
# money like found in rails I18n(see rails number_helper)
|
@@ -64,7 +64,7 @@ module KingFormat
|
|
64
64
|
format_method = "#{fld}_format_opts".to_sym
|
65
65
|
# check if the object has a custom money format method => price_total_format_opts
|
66
66
|
fopts = object && object.respond_to?(format_method) ? object.send(format_method) : opts[:currency]
|
67
|
-
strfmoney(val, fopts)
|
67
|
+
strfmoney(val, fopts, object)
|
68
68
|
elsif ( val.is_a?(Date) || (object.class.is_date_field?(fld) rescue nil) || opts[:date] )
|
69
69
|
return val if val.blank? # blank value can occur when a is_date_field is empty
|
70
70
|
# get date from opts or default or fallback into i18n
|
@@ -89,9 +89,9 @@ module KingFormat
|
|
89
89
|
# val<Number>:: the number to format
|
90
90
|
# opts<Hash{Symbol=>String}>:: Rails compatible currency formatting options,
|
91
91
|
# when nil searches default format, last exit is rails i18n
|
92
|
-
def strfmoney(val, opts=nil)
|
92
|
+
def strfmoney(val, opts=nil, object = nil)
|
93
93
|
settings = opts || default_currency_format || {}
|
94
|
-
number_to_currency(val, settings.merge({:locale => I18n.locale}))
|
94
|
+
number_to_currency(val, settings.merge({:locale => I18n.locale}).to_hash)
|
95
95
|
end
|
96
96
|
|
97
97
|
# Deprecated, to be dropped
|
@@ -107,10 +107,10 @@ module KingFormat
|
|
107
107
|
# => 7,5%
|
108
108
|
def number_to_percentage_auto_precision(number)
|
109
109
|
return nil unless number
|
110
|
-
# for now use separator
|
111
|
-
pres = (number.to_i == number.to_f) ? 0 : 1
|
112
110
|
sep = I18n.t(:'number.format.separator')
|
113
|
-
number_to_percentage(number,{
|
111
|
+
number_to_percentage(number,{ :separator=>sep,
|
112
|
+
:precision => 4,
|
113
|
+
:strip_insignificant_zeros=>true } )
|
114
114
|
end
|
115
115
|
|
116
116
|
# Translate the value of an enum field, as defined by act_as_enum
|
@@ -129,9 +129,14 @@ module KingFormat
|
|
129
129
|
# Don't translate blank value
|
130
130
|
return nil if value.blank?
|
131
131
|
#return the translation
|
132
|
-
klass.
|
132
|
+
defaults = klass.lookup_ancestors.map do |_klass|
|
133
|
+
:"#{klass.i18n_scope}.attributes.#{_klass.model_name.i18n_key}.enum.#{fieldname.to_s}.#{value.to_s}"
|
134
|
+
end
|
135
|
+
defaults << klass.human_attribute_name("enum.#{fieldname.to_s}.#{value.to_s}")
|
136
|
+
|
137
|
+
I18n.translate(default: defaults)
|
133
138
|
end
|
134
|
-
|
139
|
+
|
135
140
|
# Returns the default date formatting, as string '%d.%m.%Y'
|
136
141
|
# The returned string is passed to strftime(format)
|
137
142
|
# Override this function or set the thread var somehere in your including
|
@@ -142,7 +147,7 @@ module KingFormat
|
|
142
147
|
def default_date_format
|
143
148
|
Thread.current[:default_date_format]
|
144
149
|
end
|
145
|
-
|
150
|
+
|
146
151
|
# Returns the default currency formatting, in I18n style
|
147
152
|
# The returned hash is used in rails number_to_currency helper.
|
148
153
|
# Override this function or set the thread var somehere in your including
|
@@ -154,17 +159,17 @@ module KingFormat
|
|
154
159
|
def default_currency_format
|
155
160
|
Thread.current[:default_currency_format]
|
156
161
|
end
|
157
|
-
|
158
|
-
# Formats a number to the visible decimal places. If there are more decimal
|
162
|
+
|
163
|
+
# Formats a number to the visible decimal places. If there are more decimal
|
159
164
|
# places than the given prescision those are used.
|
160
165
|
#
|
161
166
|
# === Examples
|
162
167
|
# auto_precision(1.2340, 2)
|
163
168
|
# => "1.234"
|
164
|
-
#
|
169
|
+
#
|
165
170
|
# auto_precision(1.234500)
|
166
171
|
# => "1.2345"
|
167
|
-
#
|
172
|
+
#
|
168
173
|
# auto_precision(1.2345, 5)
|
169
174
|
# => "1.23450"
|
170
175
|
#
|
@@ -182,5 +187,5 @@ module KingFormat
|
|
182
187
|
"%01.#{precision}f" % rounded_number
|
183
188
|
end
|
184
189
|
|
185
|
-
end # FormattingHelper
|
186
|
-
end # KingFormat
|
190
|
+
end # FormattingHelper
|
191
|
+
end # KingFormat
|