king_views 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +84 -0
  3. data/Rakefile +31 -0
  4. data/VERSION +1 -0
  5. data/init.rb +3 -0
  6. data/king_form/MIT-LICENSE +20 -0
  7. data/king_form/README.rdoc +64 -0
  8. data/king_form/Rakefile +23 -0
  9. data/king_form/init.rb +1 -0
  10. data/king_form/lib/king_form.rb +19 -0
  11. data/king_form/lib/king_form/builder/base.rb +238 -0
  12. data/king_form/lib/king_form/builder/definition_list.rb +119 -0
  13. data/king_form/lib/king_form/builder/form_fields.rb +333 -0
  14. data/king_form/lib/king_form/builder/form_fields_overrides.rb +146 -0
  15. data/king_form/lib/king_form/builder/labeled.rb +116 -0
  16. data/king_form/lib/king_form/helper.rb +97 -0
  17. data/king_form/lib/king_form/nested_form_helper.rb +61 -0
  18. data/king_form/lib/king_form/overrides.rb +25 -0
  19. data/king_form/tasks/king_forms_tasks.rake +4 -0
  20. data/king_form/test/king_forms_test.rb +8 -0
  21. data/king_form/test/test_helper.rb +3 -0
  22. data/king_format/MIT-LICENSE +20 -0
  23. data/king_format/README.rdoc +20 -0
  24. data/king_format/Rakefile +23 -0
  25. data/king_format/init.rb +1 -0
  26. data/king_format/lib/helpers/date_helper.rb +25 -0
  27. data/king_format/lib/helpers/formatting_helper.rb +108 -0
  28. data/king_format/lib/helpers/money_helper.rb +63 -0
  29. data/king_format/lib/king_format.rb +14 -0
  30. data/king_format/lib/model_mixins/has_date_fields.rb +42 -0
  31. data/king_format/lib/model_mixins/has_money_fields.rb +42 -0
  32. data/king_format/lib/model_mixins/has_percent_fields.rb +34 -0
  33. data/king_format/tasks/king_format_tasks.rake +4 -0
  34. data/king_format/test/king_format_test.rb +8 -0
  35. data/king_format/test/test_helper.rb +3 -0
  36. data/king_list/MIT-LICENSE +20 -0
  37. data/king_list/README.rdoc +21 -0
  38. data/king_list/Rakefile +23 -0
  39. data/king_list/init.rb +1 -0
  40. data/king_list/lib/king_list.rb +18 -0
  41. data/king_list/lib/king_list/app_helper.rb +30 -0
  42. data/king_list/lib/king_list/builder/show.rb +71 -0
  43. data/king_list/lib/king_list/builder/table.rb +166 -0
  44. data/king_list/lib/king_list/list_helper.rb +329 -0
  45. data/king_list/lib/king_list/overrides.rb +6 -0
  46. data/king_list/tasks/king_list_tasks.rake +4 -0
  47. data/king_list/test/king_list_test.rb +8 -0
  48. data/king_list/test/test_helper.rb +3 -0
  49. data/king_views.gemspec +85 -0
  50. metadata +110 -0
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :king_forms do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class King-formsTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009-2010 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.
@@ -0,0 +1,20 @@
1
+ = KingFormat
2
+ KingFormat consists of two different formatting helper regions:
3
+
4
+ == 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.
7
+
8
+ == View & Helper
9
+ Output any model value through the function formatted_value() to ensure a consistent output.
10
+
11
+ There are select values for money-symbols and date formatting.
12
+
13
+
14
+ == Example
15
+ In a Model
16
+ has_money_fields :amount
17
+ has_date_fields :subscription_start
18
+ has_percent_fields :tax
19
+
20
+ Copyright (c) 2009 Georg Leciejewski, released under the MIT license
@@ -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_format 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_format plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'KingFormat'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1 @@
1
+ require "#{File.dirname(__FILE__)}/lib/king_format"
@@ -0,0 +1,25 @@
1
+ module KingFormat
2
+ # Provides some helper functions for action controller / view
3
+ module DateHelper
4
+
5
+ # define date formats to be used in date date_format_select
6
+ DATE_FORMATS = [
7
+ '%Y-%m-%d',
8
+ '%d/%m/%Y',
9
+ '%d.%m.%Y',
10
+ '%d-%m-%Y',
11
+ '%m/%d/%Y',
12
+ # '%d %b %Y',
13
+ # '%d %B %Y',
14
+ # '%b %d, %Y',
15
+ # '%B %d, %Y'
16
+ ]
17
+
18
+
19
+ # Default date formats to be used in select boxes => choices in rails select helper
20
+ def date_format_options
21
+ DATE_FORMATS.collect {|f| ["#{Date.today.strftime(f)} - #{f}", f]}
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,108 @@
1
+ module KingFormat
2
+ # General Helper Functions to auto-format an output
3
+ module FormattingHelper
4
+
5
+ include ActionView::Helpers::NumberHelper
6
+ # Get a nice formatted string for an object attribute value.
7
+ #
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.
12
+ # See types for more
13
+ # opts<Hash{Symbol=>String}>:: Options
14
+ #
15
+ # ==== 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
+ # Alternatively you can call the method with currency options set which will also lead to money rendering
19
+ # format -> defaults to :html returned html string has escaped html entities
20
+ #
21
+ #
22
+ # ==== AutoDetect Types
23
+ # A value is formatted according to its type which we try to detect.
24
+ # <Symbol>:: assume a value from acts_as_enum
25
+ # <DateTime|Time|Date>::I18n.localize -> l(value)
26
+ # <TrueClass|FalseClass>:: translate to yes / no
27
+ # <MoneyField String>:: coming from a has_percent_fields, formats the number with number_to_percentage_auto_precision
28
+ # <PercentField String>:: comming from has_money_fields I18n formats as money string
29
+ #
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
+ object.send(fieldname) || '' #return the content or if no content an empty string
34
+ else #field is not available in object
35
+ nil
36
+ end
37
+
38
+ # Autodetect value type
39
+ if value.nil?
40
+ nil
41
+
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
62
+ format = opts[:date] || default_date_format
63
+ format.blank? ? ::I18n.localize(value) : value.strftime(format)
64
+ else
65
+ if opts[:format] == :html
66
+ # Change HTML tag characters to entities
67
+ ERB::Util.html_escape(value)
68
+ else
69
+ value
70
+ end
71
+ end
72
+ end #formatted
73
+
74
+ # Returns the default date formatting.
75
+ # The returned string is passed to strftime(format)
76
+ # === Returns
77
+ # <String>:: strftime compatible string
78
+ def default_date_format
79
+ Company.current.date_format rescue nil
80
+ end
81
+
82
+ # Returns the default currency formatting.
83
+ # The returned hash is used in rails number_to_currency helper
84
+ # === Returns
85
+ # <Hash>:: number_to_currency compatible options hash
86
+ def default_currency_format
87
+ Company.current.currency rescue {}
88
+ end
89
+
90
+ # Formats a number to the visible decimal places if there are more than the
91
+ # given prescision
92
+ #
93
+ # ====Parameter
94
+ # number<Float>:: The number to format
95
+ # precs<Integer>:: The precision to which to round the number
96
+ # ==== Return
97
+ # nil if number is nil
98
+ # <String> with formatted number
99
+ def money_auto_precision(number, precs)
100
+ return unless number
101
+ decimals = number.to_s[/\.(.*)\z/, 1] #15.487 => 487
102
+ precision = (decimals && decimals.length > precs) ? decimals.length : precs
103
+ rounded_number = (Float(number) * (10 ** precision)).round.to_f / 10 ** precision
104
+ "%01.#{precision}f" % rounded_number
105
+ end
106
+
107
+ end # FormattingHelper
108
+ end # KingFormat
@@ -0,0 +1,63 @@
1
+ module KingFormat
2
+ # a little rewrite of the rails internal number helper with better:
3
+ # - rounding
4
+ # - detecting money symbol / money string
5
+ # -
6
+ module MoneyHelper
7
+
8
+ # returns the keys from money symbols als Hash{array} for select options
9
+ def money_selects
10
+ money_symbols.keys.sort
11
+ end
12
+
13
+ # ==== Returns
14
+ # Hash with currency as keys and formatting options as sub hash
15
+ # { 'EUR' => {:unit=>'€', :format => '%n %u', :precision=> '2',:delimiter=>'.', :separator=>','},
16
+ # 'USD' => {:unit=>'$'}
17
+ def money_symbols
18
+ @money_symbols ||= begin
19
+ eur = {:format => '%n %u', :precision=> '2',:delimiter=>'.', :separator=>','}
20
+ dol = {:format => '%n %u', :precision=> '2',:delimiter=>',', :separator=>'.'}
21
+ {
22
+ 'EUR' => eur.merge(:unit=>'€'),
23
+ 'GBP' => dol.merge(:unit=>'£'),
24
+ 'JPY' => dol.merge(:unit=>'¥'),
25
+ 'USD' => dol.merge(:unit=>'$'),
26
+ 'AUD' => dol.merge(:unit=>'$'),
27
+ 'CAD' => dol.merge(:unit=>'$'),
28
+ 'HKD' => dol.merge(:unit=>'$'),
29
+ 'SGD' => dol.merge(:unit=>'$'),
30
+ 'AED' => nil,
31
+ 'BGN' => nil,
32
+ 'CZK' => nil,
33
+ 'DKK' => nil,
34
+ 'EEK' => nil,
35
+ 'HUF' => nil,
36
+ 'LTL' => nil,
37
+ 'LVL' => nil,
38
+ 'PLN' => nil,
39
+ 'RON' => nil,
40
+ 'SEK' => nil,
41
+ 'SKK' => nil,
42
+ 'CHF' => nil,
43
+ 'ISK' => nil,
44
+ 'NOK' => nil,
45
+ 'HRK' => nil,
46
+ 'RUB' => nil,
47
+ 'TRY' => nil,
48
+ 'BRL' => nil,
49
+ 'CNY' => nil,
50
+ 'IDR' => nil,
51
+ 'KRW' => nil,
52
+ 'MXN' => nil,
53
+ 'MYR' => nil,
54
+ 'NZD' => nil,
55
+ 'PHP' => nil,
56
+ 'THB' => nil,
57
+ 'ZAR' => dol.merge(:unit=>'R')
58
+ }
59
+ end
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,14 @@
1
+ # KingFormat
2
+ $LOAD_PATH << File.expand_path(File.dirname(__FILE__))
3
+
4
+ # AR model extension to define money / percent fields
5
+ require 'model_mixins/has_percent_fields'
6
+ require 'model_mixins/has_money_fields'
7
+ require 'model_mixins/has_date_fields'
8
+
9
+ require 'helpers/formatting_helper'
10
+ ActionController::Base.helper KingFormat::FormattingHelper
11
+ require 'helpers/date_helper' # holding date functions
12
+ ActionController::Base.helper KingFormat::DateHelper
13
+ require 'helpers/money_helper' # holding money symbols
14
+ ActionController::Base.helper KingFormat::MoneyHelper
@@ -0,0 +1,42 @@
1
+ module KingFormat
2
+ module DateFields
3
+
4
+ def self.included(base)
5
+ base.send :class_inheritable_accessor, :date_fields
6
+ base.date_fields = []
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ # Defines the fields returned by self.date_fields.
12
+ # ===== Parameter
13
+ # fieldnames<Array[Symbol]>:: fieldnames/instance method names as symbols
14
+ # ==== Example
15
+ # class Invoice
16
+ # has_date_fields :total, :amout, :calculated_tax
17
+ #
18
+ def has_date_fields(*fieldnames)
19
+ self.date_fields = fieldnames
20
+ include InstanceMethods
21
+ end
22
+
23
+ # Check if a given field is declared as date field
24
+ # ==== Parameter
25
+ # fieldname<String>:: The fieldname to check. Is be casted into a symbol.
26
+ def is_date_field?(fieldname)
27
+ self.date_fields.include?(fieldname.to_sym)
28
+ end
29
+
30
+ end #ClassMethods
31
+
32
+ module InstanceMethods
33
+ # Check if a given field is declared as date field
34
+ # ==== Parameter
35
+ # fieldname<String>:: The fieldname to check. Is be casted into a symbol.
36
+ def is_date_field?(fieldname)
37
+ self.class.date_fields.include?(fieldname.to_sym)
38
+ end
39
+ end
40
+
41
+ end #Fields
42
+ end#KingFormat
@@ -0,0 +1,42 @@
1
+ module KingFormat
2
+ module MoneyFields
3
+
4
+ def self.included(base)
5
+ base.send :class_inheritable_accessor, :money_fields
6
+ base.money_fields = []
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ # Defines the fields returned by self.money_fields.
12
+ # ===== Parameter
13
+ # fieldnames<Array[Symbol]>:: fieldnames/instance method names as symbols
14
+ # ==== Example
15
+ # class Invoice
16
+ # has_money_fields :total, :amout, :calculated_tax
17
+ #
18
+ def has_money_fields(*fieldnames)
19
+ self.money_fields = fieldnames
20
+ include InstanceMethods
21
+ end
22
+
23
+ # Check if a given field is declared as money field
24
+ # ==== Parameter
25
+ # fieldname<String>:: The fieldname to check. Is be casted into a symbol.
26
+ def is_money_field?(fieldname)
27
+ self.money_fields.include?(fieldname.to_sym)
28
+ end
29
+
30
+ end #ClassMethods
31
+
32
+ module InstanceMethods
33
+ # Check if a given field is declared as money field
34
+ # ==== Parameter
35
+ # fieldname<String>:: The fieldname to check. Is casted into a symbol.
36
+ def is_money_field?(fieldname)
37
+ self.class.money_fields.include?(fieldname.to_sym)
38
+ end
39
+ end
40
+
41
+ end #Fields
42
+ end#KingFormat
@@ -0,0 +1,34 @@
1
+ module KingFormat
2
+ #TODO:
3
+ # split money / percent
4
+ # kick numeric? only used in list helper
5
+ module PercentFields
6
+
7
+ def self.included(base)
8
+ base.send :class_inheritable_accessor, :percent_fields
9
+ base.percent_fields = []
10
+ base.extend(ClassMethods)
11
+ end
12
+
13
+
14
+ module ClassMethods
15
+ # Defines the fields returned by self.percent_fields.
16
+ # ===== Parameter
17
+ # fieldnames<Array[Symbol]>:: fieldnames/instance method names as symbols
18
+ # ==== Example
19
+ # class Invoice
20
+ # has_percent_fields :total, :amout, :calculated_tax
21
+ #
22
+ def has_percent_fields(*fieldnames)
23
+ self.percent_fields = fieldnames
24
+ #include InstanceMethods
25
+ end
26
+
27
+ # Check if a given field is declared as percent
28
+ def is_percent_field?(fieldname)
29
+ self.percent_fields.include?(fieldname) if self.respond_to?(:percent_fields)
30
+ end
31
+ end
32
+
33
+ end #Fields
34
+ end#KingFormat
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :king_format do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class KingFormatTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'