simple_show_helper 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/Gemfile +5 -0
  2. data/Gemfile.lock +20 -2
  3. data/Rakefile +0 -10
  4. data/VERSION +1 -1
  5. data/lib/simple_show_helper.rb +85 -48
  6. metadata +33 -11
data/Gemfile CHANGED
@@ -6,3 +6,8 @@ group :development do
6
6
  gem "jeweler", "~> 1.6.4"
7
7
  gem "simplecov", ">= 0"
8
8
  end
9
+
10
+ group :development, :test do
11
+ gem 'sqlite3'
12
+ gem "activerecord"
13
+ end
@@ -1,14 +1,28 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ activemodel (3.2.9)
5
+ activesupport (= 3.2.9)
6
+ builder (~> 3.0.0)
7
+ activerecord (3.2.9)
8
+ activemodel (= 3.2.9)
9
+ activesupport (= 3.2.9)
10
+ arel (~> 3.0.2)
11
+ tzinfo (~> 0.3.29)
12
+ activesupport (3.2.9)
13
+ i18n (~> 0.6)
14
+ multi_json (~> 1.0)
15
+ arel (3.0.2)
16
+ builder (3.0.4)
4
17
  diff-lcs (1.1.3)
5
18
  git (1.2.5)
19
+ i18n (0.6.1)
6
20
  jeweler (1.6.4)
7
21
  bundler (~> 1.0)
8
22
  git (>= 1.2.5)
9
23
  rake
10
- multi_json (1.3.7)
11
- rake (10.0.1)
24
+ multi_json (1.5.0)
25
+ rake (10.0.3)
12
26
  rspec (2.3.0)
13
27
  rspec-core (~> 2.3.0)
14
28
  rspec-expectations (~> 2.3.0)
@@ -21,12 +35,16 @@ GEM
21
35
  multi_json (~> 1.0)
22
36
  simplecov-html (~> 0.7.1)
23
37
  simplecov-html (0.7.1)
38
+ sqlite3 (1.3.6)
39
+ tzinfo (0.3.35)
24
40
 
25
41
  PLATFORMS
26
42
  ruby
27
43
 
28
44
  DEPENDENCIES
45
+ activerecord
29
46
  bundler (~> 1.0.0)
30
47
  jeweler (~> 1.6.4)
31
48
  rspec (~> 2.3.0)
32
49
  simplecov
50
+ sqlite3
data/Rakefile CHANGED
@@ -42,16 +42,6 @@ end
42
42
 
43
43
  task :default => :spec
44
44
 
45
- require 'rake/rdoctask'
46
- Rake::RDocTask.new do |rdoc|
47
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
-
49
- rdoc.rdoc_dir = 'rdoc'
50
- rdoc.title = "gpx2exif #{version}"
51
- rdoc.rdoc_files.include('README*')
52
- rdoc.rdoc_files.include('lib/**/*.rb')
53
- end
54
-
55
45
  desc "Run RSpec with code coverage"
56
46
  task :coverage do
57
47
  `rake spec COVERAGE=true`
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -1,46 +1,59 @@
1
- module ApplicationHelper
2
- # Usage:
3
- # use in view with bootstrap
4
- # = simple_show_helper(User.first)
5
- #
6
- # Keep in mind:
7
- # * it uses only locales in format like this 'en.user.email'
8
- #
9
- # Custom attributes:
10
- # = simple_show_helper(User.first, attrs: ["email", "name"])
11
- # Add created_at, updated_at:
12
- # = simple_show_helper(User.first, timestamps: true)
13
- # Custom table class
14
- # = simple_show_helper(User.first, table_class: "table")
15
- #
16
- # Example:
17
- #= raw simple_show_helper(resource, attrs: ["name"], timestamps: true, table_class: "table")
1
+ class SimpleShowHelper
2
+ def humanize(o)
3
+ return case o.class.to_s
4
+ when "Time", "ActiveSupport::TimeWithZone" then
5
+ l(o, format: :long)
6
+ when "TrueClass" then
7
+ I18n.t(:true)
8
+ when "FalseClass" then
9
+ I18n.t(:no)
10
+ when "Hash" then
11
+ d = Array.new
12
+ o.keys.sort.each do |k|
13
+ d << [k, humanize(o[k])]
14
+ end
15
+ render_table(d, @table_class)
16
+ when NilClass
17
+ '...'
18
+ else
19
+ o.to_s
20
+ end
18
21
 
19
- def simple_show_helper(m, options = { })
20
- if options[:table_class]
21
- table_class = options[:table_class]
22
- else
23
- table_class = "table table-striped table-bordered"
22
+ end
23
+
24
+ def render_table(data, table_class)
25
+ s = "<table class=\"#{table_class}\">"
26
+ data.each do |dat|
27
+ s += "<tr>\n"
28
+ dat.each do |d|
29
+ s += "<td>#{d}</td>\n"
30
+ end
31
+ s += "</tr>\n"
24
32
  end
33
+ s += "</table>"
25
34
 
35
+ return s
36
+ end
37
+
38
+ def scan_object
26
39
  # column used to describe association
27
40
  association_desc_keys = ["name", "email", "desc", "type", "id"]
28
41
  special_attrs = ["id", "created_at", "updated_at"]
29
- object_tableize = m.class.to_s.tableize.singularize
42
+ object_tableize = @object.class.to_s.tableize.singularize
30
43
 
31
44
  # foreign keys, beware! magic!
32
- assoc_keys = m.class.reflect_on_all_associations.collect { |a| a.foreign_key }.uniq & m.attributes.keys
45
+ assoc_keys = @object.class.reflect_on_all_associations.collect { |a| a.foreign_key }.uniq & @object.attributes.keys
33
46
  # associations classes
34
47
  associations = Hash.new
35
48
  assoc_keys.each do |ak|
36
- associations[ak] = m.class.reflect_on_all_associations.select { |a| a.foreign_key == ak }.first.klass
49
+ associations[ak] = @object.class.reflect_on_all_associations.select { |a| a.foreign_key == ak }.first.klass
37
50
  end
38
51
 
39
- if options[:attrs]
40
- attrs = options[:attrs].collect { |k| k.to_s }
52
+ if @options[:attrs]
53
+ attrs = @options[:attrs].collect { |k| k.to_s }
41
54
  else
42
55
  # remove special attributes
43
- attrs = m.attributes.keys.sort
56
+ attrs = @object.attributes.keys.sort
44
57
  special_attrs.each do |s|
45
58
  attrs.delete(s)
46
59
  end
@@ -50,19 +63,9 @@ module ApplicationHelper
50
63
  details = Array.new
51
64
  attrs.each do |d|
52
65
  # key = I18n.t("#{object_tableize}.#{d}")
53
- key = m.class.human_attribute_name(d)
54
- value = m.attributes[d]
55
-
56
- value = case value.class.to_s
57
- when "Time", "ActiveSupport::TimeWithZone" then
58
- l(value, format: :long)
59
- when "TrueClass" then
60
- I18n.t(:true)
61
- when "FalseClass" then
62
- I18n.t(:no)
63
- else
64
- value
65
- end
66
+ key = @object.class.human_attribute_name(d)
67
+ value = @object.attributes[d]
68
+
66
69
 
67
70
  if assoc_keys.include?(d)
68
71
  # nobody wants id
@@ -94,19 +97,53 @@ module ApplicationHelper
94
97
  end
95
98
 
96
99
  # timestamps
97
- if options[:timestamps]
100
+ if @options[:timestamps]
98
101
  #details << [m.class.human_attribute_name("created_at"), l(m.attributes["created_at"], format: :long)]
99
102
  #details << [m.class.human_attribute_name("updated_at"), l(m.attributes["updated_at"], format: :long)]
100
- details << [I18n.t("created_at"), l(m.attributes["created_at"], format: :long)]
101
- details << [I18n.t("updated_at"), l(m.attributes["updated_at"], format: :long)]
103
+ details << [I18n.t("created_at"), @object.attributes["created_at"]]
104
+ details << [I18n.t("updated_at"), @object.attributes["updated_at"]]
102
105
  end
103
106
 
104
- s = "<table class=\"#{table_class}\">"
107
+ @details = Array.new
105
108
  details.each do |d|
106
- s += "<tr><td>#{d[0]}</td><td>#{d[1]}</td></tr>\n"
109
+ @details << [humanize(d[0]), humanize(d[1])]
107
110
  end
108
- s += "</table>"
111
+ end
109
112
 
110
- return s
113
+ def initialize(_object, _options)
114
+ @object = _object
115
+ @options = _options
116
+ @table_class = _options[:table_class] || "table table-striped table-bordered"
117
+
118
+ scan_object
111
119
  end
120
+
121
+ def to_s
122
+ @s = render_table(@details, @table_class)
123
+ return @s
124
+ end
125
+ end
126
+
127
+ module ApplicationHelper
128
+ # Usage:
129
+ # use in view with bootstrap
130
+ # = simple_show_helper(User.first)
131
+ #
132
+ # Keep in mind:
133
+ # * it uses only locales in format like this 'en.user.email'
134
+ #
135
+ # Custom attributes:
136
+ # = simple_show_helper(User.first, attrs: ["email", "name"])
137
+ # Add created_at, updated_at:
138
+ # = simple_show_helper(User.first, timestamps: true)
139
+ # Custom table class
140
+ # = simple_show_helper(User.first, table_class: "table")
141
+ #
142
+ # Example:
143
+ #= raw simple_show_helper(resource, attrs: ["name"], timestamps: true, table_class: "table")
144
+
145
+ def simple_show_helper(m, options = { })
146
+ return SimpleShowHelper.new(m, options).to_s
147
+ end
148
+
112
149
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_show_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-16 00:00:00.000000000Z
12
+ date: 2012-12-16 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &24722540 !ruby/object:Gem::Requirement
16
+ requirement: &28121760 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.3.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *24722540
24
+ version_requirements: *28121760
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &24721980 !ruby/object:Gem::Requirement
27
+ requirement: &28121240 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *24721980
35
+ version_requirements: *28121240
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &24721460 !ruby/object:Gem::Requirement
38
+ requirement: &28120720 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.6.4
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *24721460
46
+ version_requirements: *28120720
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: simplecov
49
- requirement: &24720960 !ruby/object:Gem::Requirement
49
+ requirement: &28120220 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,29 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *24720960
57
+ version_requirements: *28120220
58
+ - !ruby/object:Gem::Dependency
59
+ name: sqlite3
60
+ requirement: &28119680 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *28119680
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: &28119160 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *28119160
58
80
  description: Simple helper to create 'show' pages easily.
59
81
  email: bobikx@poczta.fm
60
82
  executables: []
@@ -85,7 +107,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
107
  version: '0'
86
108
  segments:
87
109
  - 0
88
- hash: -74933958268505984
110
+ hash: 690038885608953810
89
111
  required_rubygems_version: !ruby/object:Gem::Requirement
90
112
  none: false
91
113
  requirements: