king_views 1.0.5 → 1.1.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/README.rdoc CHANGED
@@ -28,6 +28,8 @@ Clean up your forms with helpers in rails haml views, with support for:
28
28
  * bundle a couple of inputs under one label or dt incl. css class based counter for the wrapper
29
29
  * based on rails action view helpers so all parameters are passed through(nearly all) to the original versions
30
30
 
31
+ Also see README in KingForm: http://github.com/salesking/king_views/tree/master/king_form/
32
+
31
33
  === Example
32
34
  - dl_form_for @payment, :url => 'a path', :html => { :method => :post } do |f|
33
35
  -f.section do
@@ -53,23 +55,29 @@ extensive as KingForm and only provides the following:
53
55
  * dl-helper for detail views
54
56
  * action_icons & action_buttons(forms) for css enabled icon markup
55
57
 
56
- === Example
58
+ Also see README in KingList: http://github.com/salesking/king_views/tree/master/king_list/
59
+
60
+ === Quick Example
61
+
57
62
  # Definition list for detail views
58
63
  - dl_for @invoice do |f|
59
64
  = f.show :number
60
- = f.show :date # auto-formated date field
61
- = f.show :total # auto-formated money field
65
+ = f.show :date # auto-formated date field see KingFormat
66
+ = f.show :total # auto-formated money field see KingFormat
62
67
 
63
- # table with header-klick sorting disabled
68
+ # table with header-klick sorting disabled and icons in first column
64
69
  - table_for(@payments,{:sorting => false} ) do |t, payment|
65
70
  - t.action_column do
71
+ = action_icon :edit, edit_payment_path(payment)
66
72
  = action_button 'delete', {:url=> 'some-path', :title => t(:'link.delete'), :method => :delete, :class=>'delete'}
67
73
  = t.column :date
68
74
  = t.column :amount, :td_options => {opt=>val}
69
75
 
70
76
  == KingFormat
71
77
  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.
78
+ types are defined inside each model. And the view helpers will show a nice output.
79
+
80
+ Also see README http://github.com/salesking/king_views/tree/master/king_format/
73
81
 
74
82
  === Example:
75
83
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.5
1
+ 1.1.0
@@ -8,7 +8,7 @@ module KingForm
8
8
  # Generate input tag with title for text editing
9
9
  # ==== Example
10
10
  # 1) text :name
11
- # 2) text :name, :title => 'Your Name:', :value => @the_name, :size=>30, .maxlength=>35
11
+ # 2) text :name, :title => 'Your Name:', :value => @the_name, :size=>30, :maxlength=>35
12
12
  #
13
13
  # ==== Parameter
14
14
  # fieldname<Symbol, String>:: The field name of the current object
@@ -268,7 +268,7 @@ module KingForm
268
268
  end
269
269
 
270
270
  title = options.delete(:title) || build_title(fieldname)
271
- value = options.delete(:value) || @template.formatted_value(current_object, fieldname)
271
+ value = options.delete(:value) || @template.strfval(current_object, fieldname)
272
272
 
273
273
  if info_text = options.delete(:info) #add info tag if info test given
274
274
  value << info_tag(info_text)
@@ -1,6 +1,7 @@
1
1
  require 'test_helper'
2
-
3
- class King-formsTest < ActiveSupport::TestCase
2
+ # yes i know ... big TODO, currently implicitly tested at salesking.eu which
3
+ # makes pretty heavy use of it
4
+ class KingFormTest < ActiveSupport::TestCase
4
5
  # Replace this with your real tests.
5
6
  test "the truth" do
6
7
  assert true
@@ -1,20 +1,37 @@
1
1
  = KingFormat
2
+
3
+ Define fields being of type date, currency or percent. When the fields are shown
4
+ within KingList/KingForm views, their format is detected and the output is
5
+ automatically formatted. Default formats of date and currency fields can be set
6
+ and both also take te current I18n.locale into account.
7
+
2
8
  KingFormat consists of two different formatting helper regions:
3
9
 
4
10
  == Model
5
- Define fields being of type date, money or percent. When the fields are shown in the
6
- views, their format is detected and the output is looking nice.
11
+
12
+ This is where you define the type of fields/attributes. Of course the model
13
+ should somehow respond to those method names.
14
+
15
+ class Document
16
+ has_money_fields :gross_total, :net_total
17
+ has_date_fields :created_at, :date
18
+ has_percent_fields :discount
19
+ ...
20
+ end
7
21
 
8
22
  == View & Helper
9
- Output any model value through the function formatted_value() to ensure a consistent output.
10
23
 
11
- There are select values for money-symbols and date formatting.
24
+ Output any model value through the function strfval() to ensure a consistent output.
25
+ Further on you find select values for money-symbols and date formatting.
26
+
27
+ A simple HAML example, without hussles:
12
28
 
29
+ - dl_for(current_object) do |f|
30
+ = f.show :net_total
31
+ = f.show :gross_total
32
+ = f.show :date
33
+ = f.show :discount
34
+ = f.show :net_total, :value=>strfval(current_object, :net_total)
13
35
 
14
- == Example
15
- In a Model
16
- has_money_fields :amount
17
- has_date_fields :subscription_start
18
- has_percent_fields :tax
19
36
 
20
- Copyright (c) 2009 Georg Leciejewski, released under the MIT license
37
+ Copyright (c) 2009-2010 Georg Leciejewski, released under the MIT license
@@ -6,19 +6,21 @@ module KingFormat
6
6
  # Get a nice formatted string for an object attribute value.
7
7
  #
8
8
  # ==== Parameters
9
- # object<Object>:: The object on which the given fieldname will be called => object.send(fieldname), to get the value
10
- # fieldname<String>:: Name of the field for which to get a value
11
- # value<String|Symbol|Date|FalseClass|TrueClass|...>::The value to display. When set this will be used instead of asking the object for its value.
9
+ # object<Object>:: The object on which the given fld name will be called
10
+ # => object.send(fld), to get the value
11
+ # fld<String>:: Name of the field for which to get a value
12
+ # val<String|Symbol|Date|FalseClass|TrueClass|...>::The value to display.
13
+ # When set this will be used instead of asking the object for its value.
12
14
  # See types for more
13
15
  # opts<Hash{Symbol=>String}>:: Options
14
16
  #
15
17
  # ==== Options opts
16
- # currency<Hash{Symbol=>String}>:: Currency settings to format string as a money like found in rails I18n. or see rails number_helper.
17
- # When set, object AND fieldname must be set and the field must be in money_fields (via: has_money_fields)
18
+ # currency<Hash{Symbol=>String}>:: Currency settings to format string as a money like found in rails I18n.
19
+ # or see rails number_helper.
20
+ # When set, object AND fld must be set and the field must be in money_fields (via: has_money_fields)
18
21
  # Alternatively you can call the method with currency options set which will also lead to money rendering
19
22
  # format -> defaults to :html returned html string has escaped html entities
20
23
  #
21
- #
22
24
  # ==== AutoDetect Types
23
25
  # A value is formatted according to its type which we try to detect.
24
26
  # <Symbol>:: assume a value from acts_as_enum
@@ -27,81 +29,92 @@ module KingFormat
27
29
  # <MoneyField String>:: coming from a has_percent_fields, formats the number with number_to_percentage_auto_precision
28
30
  # <PercentField String>:: comming from has_money_fields I18n formats as money string
29
31
  #
30
- def formatted_value(object, fieldname, value=nil, opts={})
31
- # If no value given, call fieldname on object to get the current value
32
- value ||= if object.respond_to?(fieldname)
33
- #return the content(a pointer it) or an empty string.
34
- object.send(fieldname) || ''
35
- else #field is not available in object
36
- nil
37
- end
38
-
32
+ def strfval (object, fld, val=nil, opts={})
33
+ # If no value given, call fld on object to get the current value
34
+ #return the content(a pointer) or an empty string OR nil of field is not available
35
+ val ||= object.respond_to?(fld) ? ( object.send(fld) || '') : nil
39
36
  # Autodetect value type
40
- if value.nil?
37
+ if val.nil?
41
38
  nil
42
- elsif value.is_a?(Symbol) # enum value from acts_as_enum
43
- translated_enum_value(object, fieldname, value)
44
- elsif value.is_a?(DateTime) || value.is_a?(Time) #|| value.is_a?(Date)
45
- I18n.localize(value)
46
- elsif value.is_a?(TrueClass) || value.is_a?(FalseClass)
47
- value ? t(:'sk.yes') : t(:'sk.no')
48
- elsif (object.class.is_percent_field?(fieldname) rescue nil)
49
- (value && !value.blank?) ? number_to_percentage_auto_precision(value) : ''
50
- elsif (object.class.is_money_field?(fieldname) rescue nil) || opts[:currency] # field is defined as money field OR currency options are passed in
51
- # get currency from opts or company or fallback into i18n
52
- settings = opts[:currency] || default_currency_format
53
- # display with a rounding of 2 despite precision defined in company settings .. quick and dirty
54
- # .. other option would be to define such in has_money_fields via method missing or so
55
- if fieldname.to_s[/_round$/] # invoice.price_total_round
56
- settings[:precision] = 2
57
- end
58
- number_to_currency(value, settings.merge({:locale => I18n.locale}))
59
- elsif ( value.is_a?(Date) || (object.class.is_date_field?(fieldname) rescue nil) || opts[:date] ) #field is defined as date field OR date options are passed in
60
- return value if value.blank? # blank value can occur when a is_date_field is empty
61
- # get date from opts or company or fallback into i18n
39
+ elsif val.is_a?(Symbol) # enum value from acts_as_enum
40
+ translated_enum_value(object, fld, val)
41
+ elsif val.is_a?(DateTime) || val.is_a?(Time) #|| val.is_a?(Date)
42
+ I18n.localize(val)
43
+ elsif val.is_a?(TrueClass) || val.is_a?(FalseClass)
44
+ val ? t(:'sk.yes') : t(:'sk.no')
45
+ elsif (object.class.is_percent_field?(fld) rescue nil)
46
+ (val && !val.blank?) ? number_to_percentage_auto_precision(val) : ''
47
+ elsif (object.class.is_money_field?(fld) rescue nil) || opts[:currency]
48
+ # field is defined as money field OR currency options are passed in
49
+ strfmoney(val, opts[:currency])
50
+ elsif ( val.is_a?(Date) || (object.class.is_date_field?(fld) rescue nil) || opts[:date] )
51
+ # field is defined as date field OR date options are passed in
52
+ return val if val.blank? # blank value can occur when a is_date_field is empty
53
+ # get date from opts or default or fallback into i18n
62
54
  format = opts[:date] || default_date_format
63
- format.blank? ? ::I18n.localize(value) : value.strftime(format)
55
+ format.blank? ? ::I18n.localize(val) : val.strftime(format)
64
56
  else
65
57
  if opts[:format] == :html
66
58
  # Change HTML tag characters to entities
67
- ERB::Util.html_escape(value)
59
+ html_escape(val) # from Haml::Helpers
68
60
  else
69
- #Copy->val.dup because obj.send(fieldname) returns pointer, and subsequent calls
70
- #may then alter the real value f.ex val = formatted_value(yx) + "info text"
61
+ #Copy->val.dup because obj.send(fld) returns pointer, and subsequent calls
62
+ #may then alter the real value f.ex val = strfval(yx) + "info text"
71
63
  # rescue for Fixnums are not dup -able, cause its a call by value
72
- value.blank? ? value : ( value.dup rescue value )
64
+ val.blank? ? val : ( val.dup rescue val )
73
65
  end
74
66
  end
75
- end #formatted
67
+ end #strfval
68
+
69
+ # Formats the given value using rails number_to_currency. Get currency
70
+ # from options hash or @default_currency_format or i18n as fallback
71
+ def strfmoney(val, opts={})
72
+ settings = opts || default_currency_format
73
+ number_to_currency(val, settings.merge({:locale => I18n.locale}))
74
+ end
75
+
76
+ # Deprecated, to be dropped
77
+ def formatted_value(object, fld, val=nil, opts={})
78
+ ::ActiveSupport::Deprecation.warn('"formatted_value" has been deprecated, use the "strfval" instead. Func will be removed within next releases', caller)
79
+ strfval(object, fld, val, opts)
80
+ end
76
81
 
77
82
  # Returns the default date formatting.
78
83
  # The returned string is passed to strftime(format)
79
- # TODO: NoGo since one cannot expect a company to be present
80
84
  # === Returns
81
85
  # <String>:: strftime compatible string
82
86
  def default_date_format
83
- Company.current.date_format rescue nil
87
+ @default_date_format || {}
84
88
  end
85
-
86
- # Returns the default currency formatting from Company.current => Thread var
87
- # TODO: NoGo since one cannot expect a company to be present
89
+
90
+ # Returns the default currency formatting
88
91
  # The returned hash is used in rails number_to_currency helper
89
92
  # === Returns
90
93
  # <Hash>:: number_to_currency compatible options hash
91
94
  def default_currency_format
92
- Company.current.currency rescue {}
95
+ @default_currency_format || {}
93
96
  end
94
-
95
- # Formats a number to the visible decimal places if there are more than the
96
- # given prescision
97
- #
97
+
98
+ # Formats a number to the visible decimal places. If there are more decimal
99
+ # places than the given prescision those are used.
100
+ #
101
+ # === Examples
102
+ # auto_precision(1.2340, 2)
103
+ # => "1.234"
104
+ #
105
+ # auto_precision(1.234500)
106
+ # => "1.2345"
107
+ #
108
+ # auto_precision(1.2345, 5)
109
+ # => "1.23450"
110
+ #
98
111
  # ====Parameter
99
112
  # number<Float>:: The number to format
100
113
  # precs<Integer>:: The precision to which to round the number
101
114
  # ==== Return
102
115
  # nil if number is nil
103
- # <String> with formatted number
104
- def money_auto_precision(number, precs)
116
+ # <Float> with formatted number
117
+ def auto_precision(number, precs=2)
105
118
  return unless number
106
119
  decimals = number.to_s[/\.(.*)\z/, 1] #15.487 => 487
107
120
  precision = (decimals && decimals.length > precs) ? decimals.length : precs
@@ -110,4 +123,5 @@ module KingFormat
110
123
  end
111
124
 
112
125
  end # FormattingHelper
126
+
113
127
  end # KingFormat
@@ -1,3 +1,4 @@
1
+ require 'active_support/deprecation'
1
2
  # KingFormat
2
3
  $LOAD_PATH << File.expand_path(File.dirname(__FILE__))
3
4
 
@@ -1,5 +1,6 @@
1
1
  require 'test_helper'
2
-
2
+ # yes i know ... big TODO, currently implicitly tested at salesking.eu which
3
+ # makes pretty heavy use of it
3
4
  class KingFormatTest < ActiveSupport::TestCase
4
5
  # Replace this with your real tests.
5
6
  test "the truth" do
@@ -49,7 +49,7 @@ module KingList
49
49
  caption = options.delete(:caption) || object.class.human_attribute_name(field_name.to_s)
50
50
 
51
51
  # Use given value or take formatted value as default
52
- value = options.delete(:value) || @template.formatted_value(object, field_name) || '&nbsp;'
52
+ value = options.delete(:value) || @template.strfval(object, field_name) || '&nbsp;'
53
53
 
54
54
  # if link option is set, then link to this object
55
55
  if link = options.delete(:link)
@@ -90,7 +90,7 @@ module KingList
90
90
 
91
91
  when :content
92
92
  # Use given value (or formatted value as default)
93
- value = options.delete(:value) || @template.formatted_value(object, field_name, value)
93
+ value = options.delete(:value) || @template.strfval(object, field_name, value)
94
94
 
95
95
  # If link option is set, then link to this
96
96
  # === Example #
@@ -1,5 +1,6 @@
1
1
  require 'test_helper'
2
-
2
+ # yes i know ... big TODO, currently implicitly tested at salesking.eu which
3
+ # makes pretty heavy use of it
3
4
  class KingListTest < ActiveSupport::TestCase
4
5
  # Replace this with your real tests.
5
6
  test "the truth" do
data/king_views.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{king_views}
8
- s.version = "1.0.5"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Georg Leciejewski"]
12
- s.date = %q{2010-08-31}
12
+ s.date = %q{2010-09-25}
13
13
  s.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. }
14
14
  s.email = %q{gl@salesking.eu}
15
15
  s.extra_rdoc_files = [
@@ -21,11 +21,9 @@ Gem::Specification.new do |s|
21
21
  "README.rdoc",
22
22
  "Rakefile",
23
23
  "VERSION",
24
- "init.rb",
25
24
  "king_form/MIT-LICENSE",
26
25
  "king_form/README.rdoc",
27
26
  "king_form/Rakefile",
28
- "king_form/init.rb",
29
27
  "king_form/lib/king_form.rb",
30
28
  "king_form/lib/king_form/builder/base.rb",
31
29
  "king_form/lib/king_form/builder/definition_list.rb",
@@ -35,13 +33,11 @@ Gem::Specification.new do |s|
35
33
  "king_form/lib/king_form/helper.rb",
36
34
  "king_form/lib/king_form/nested_form_helper.rb",
37
35
  "king_form/lib/king_form/overrides.rb",
38
- "king_form/tasks/king_forms_tasks.rake",
39
36
  "king_form/test/king_forms_test.rb",
40
37
  "king_form/test/test_helper.rb",
41
38
  "king_format/MIT-LICENSE",
42
39
  "king_format/README.rdoc",
43
40
  "king_format/Rakefile",
44
- "king_format/init.rb",
45
41
  "king_format/lib/helpers/date_helper.rb",
46
42
  "king_format/lib/helpers/formatting_helper.rb",
47
43
  "king_format/lib/helpers/money_helper.rb",
@@ -49,20 +45,17 @@ Gem::Specification.new do |s|
49
45
  "king_format/lib/model_mixins/has_date_fields.rb",
50
46
  "king_format/lib/model_mixins/has_money_fields.rb",
51
47
  "king_format/lib/model_mixins/has_percent_fields.rb",
52
- "king_format/tasks/king_format_tasks.rake",
53
48
  "king_format/test/king_format_test.rb",
54
49
  "king_format/test/test_helper.rb",
55
50
  "king_list/MIT-LICENSE",
56
51
  "king_list/README.rdoc",
57
52
  "king_list/Rakefile",
58
- "king_list/init.rb",
59
53
  "king_list/lib/king_list.rb",
60
54
  "king_list/lib/king_list/app_helper.rb",
61
55
  "king_list/lib/king_list/builder/show.rb",
62
56
  "king_list/lib/king_list/builder/table.rb",
63
57
  "king_list/lib/king_list/list_helper.rb",
64
58
  "king_list/lib/king_list/overrides.rb",
65
- "king_list/tasks/king_list_tasks.rake",
66
59
  "king_list/test/king_list_test.rb",
67
60
  "king_list/test/test_helper.rb",
68
61
  "king_views.gemspec",
data/lib/king_views.rb CHANGED
@@ -1,3 +1,3 @@
1
- require "#{File.dirname(__FILE__)}/../king_format/init"
2
- require "#{File.dirname(__FILE__)}/../king_list/init"
3
- require "#{File.dirname(__FILE__)}/../king_form/init"
1
+ require "#{File.dirname(__FILE__)}/../king_format/lib/king_format"
2
+ require "#{File.dirname(__FILE__)}/../king_list/lib/king_list"
3
+ require "#{File.dirname(__FILE__)}/../king_form/lib/king_form"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: king_views
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 5
10
- version: 1.0.5
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Georg Leciejewski
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-31 00:00:00 +02:00
18
+ date: 2010-09-25 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -33,11 +33,9 @@ files:
33
33
  - README.rdoc
34
34
  - Rakefile
35
35
  - VERSION
36
- - init.rb
37
36
  - king_form/MIT-LICENSE
38
37
  - king_form/README.rdoc
39
38
  - king_form/Rakefile
40
- - king_form/init.rb
41
39
  - king_form/lib/king_form.rb
42
40
  - king_form/lib/king_form/builder/base.rb
43
41
  - king_form/lib/king_form/builder/definition_list.rb
@@ -47,13 +45,11 @@ files:
47
45
  - king_form/lib/king_form/helper.rb
48
46
  - king_form/lib/king_form/nested_form_helper.rb
49
47
  - king_form/lib/king_form/overrides.rb
50
- - king_form/tasks/king_forms_tasks.rake
51
48
  - king_form/test/king_forms_test.rb
52
49
  - king_form/test/test_helper.rb
53
50
  - king_format/MIT-LICENSE
54
51
  - king_format/README.rdoc
55
52
  - king_format/Rakefile
56
- - king_format/init.rb
57
53
  - king_format/lib/helpers/date_helper.rb
58
54
  - king_format/lib/helpers/formatting_helper.rb
59
55
  - king_format/lib/helpers/money_helper.rb
@@ -61,20 +57,17 @@ files:
61
57
  - king_format/lib/model_mixins/has_date_fields.rb
62
58
  - king_format/lib/model_mixins/has_money_fields.rb
63
59
  - king_format/lib/model_mixins/has_percent_fields.rb
64
- - king_format/tasks/king_format_tasks.rake
65
60
  - king_format/test/king_format_test.rb
66
61
  - king_format/test/test_helper.rb
67
62
  - king_list/MIT-LICENSE
68
63
  - king_list/README.rdoc
69
64
  - king_list/Rakefile
70
- - king_list/init.rb
71
65
  - king_list/lib/king_list.rb
72
66
  - king_list/lib/king_list/app_helper.rb
73
67
  - king_list/lib/king_list/builder/show.rb
74
68
  - king_list/lib/king_list/builder/table.rb
75
69
  - king_list/lib/king_list/list_helper.rb
76
70
  - king_list/lib/king_list/overrides.rb
77
- - king_list/tasks/king_list_tasks.rake
78
71
  - king_list/test/king_list_test.rb
79
72
  - king_list/test/test_helper.rb
80
73
  - king_views.gemspec
data/init.rb DELETED
@@ -1,3 +0,0 @@
1
- require "#{File.dirname(__FILE__)}/king_format/init"
2
- require "#{File.dirname(__FILE__)}/king_list/init"
3
- require "#{File.dirname(__FILE__)}/king_form/init"
data/king_form/init.rb DELETED
@@ -1 +0,0 @@
1
- require "#{File.dirname(__FILE__)}/lib/king_form"
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :king_forms do
3
- # # Task goes here
4
- # end
data/king_format/init.rb DELETED
@@ -1 +0,0 @@
1
- require "#{File.dirname(__FILE__)}/lib/king_format"
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :king_format do
3
- # # Task goes here
4
- # end
data/king_list/init.rb DELETED
@@ -1 +0,0 @@
1
- require "#{File.dirname(__FILE__)}/lib/king_list"
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :king_list do
3
- # # Task goes here
4
- # end