simple_show_helper 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "rspec", "~> 2.3.0"
5
+ gem "bundler", "~> 1.0.0"
6
+ gem "jeweler", "~> 1.6.4"
7
+ gem "simplecov", ">= 0"
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.6.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ multi_json (1.3.7)
11
+ rake (10.0.1)
12
+ rspec (2.3.0)
13
+ rspec-core (~> 2.3.0)
14
+ rspec-expectations (~> 2.3.0)
15
+ rspec-mocks (~> 2.3.0)
16
+ rspec-core (2.3.1)
17
+ rspec-expectations (2.3.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.3.0)
20
+ simplecov (0.7.1)
21
+ multi_json (~> 1.0)
22
+ simplecov-html (~> 0.7.1)
23
+ simplecov-html (0.7.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.0.0)
30
+ jeweler (~> 1.6.4)
31
+ rspec (~> 2.3.0)
32
+ simplecov
data/LICENSE.txt ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ simple_show_helper
2
+ =======
3
+
4
+ Why?
5
+ -----
6
+
7
+ It is intended to use with new Rails+Bootstrap apps to create as fast as possible 'show' pages.
8
+ You only have to add one line to view which will describe model instance + it references.
9
+
10
+ I use this tool here https://github.com/akwiatkowski/waypoint_manager/blob/master/app/views/areas/show.html.haml
11
+
12
+ How to?
13
+ -----
14
+
15
+ Add line in view
16
+
17
+ = simple_model_details(User.first)
18
+
19
+ Keep in mind that it uses only locales in format for example: 'en.user.email'
20
+
21
+ Custom attributes:
22
+
23
+ simple_model_details(User.first, attrs: ["email", "name"])
24
+
25
+ Add created_at, updated_at attributes:
26
+
27
+ simple_model_details(User.first, timestamps: true)
28
+
29
+ Custom table class, for not Bootstrap users:
30
+
31
+ simple_model_details(User.first, table_class: "table")
32
+
33
+ All options example:
34
+
35
+ raw simple_model_details(resource, attrs: ["name"], timestamps: true, table_class: "table")
36
+
37
+
38
+ Spec, tests, ...
39
+ ----------------
40
+
41
+ ... :)
42
+
43
+
44
+ Contributing to simple_show_helper
45
+ -------------------------------
46
+
47
+ [![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=bobik314&url=https://github.com/akwiatkowski/simple_show_helper&title=simple_show_helper&language=en_GB&tags=github&category=software)
48
+
49
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
50
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
51
+ * Fork the project
52
+ * Start a feature/bugfix branch
53
+ * Commit and push until you are happy with your contribution
54
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
55
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
56
+
57
+
58
+ Copyright
59
+ ---------
60
+
61
+ Copyright (c) 2012 Aleksander Kwiatkowski. See LICENSE.txt for
62
+ further details.
63
+
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "simple_show_helper"
18
+ gem.homepage = "http://github.com/akwiatkowski/simple_show_helper"
19
+ gem.license = "LGPLv3"
20
+ gem.summary = %Q{Simple helper to create 'show' pages easily}
21
+ gem.description = %Q{Simple helper to create 'show' pages easily.}
22
+ gem.email = "bobikx@poczta.fm"
23
+ gem.authors = ["Aleksander Kwiatkowski"]
24
+ # dependencies defined in Gemfile
25
+
26
+ gem.files = FileList[
27
+ "[A-Z]*", "{bin,generators,lib,test}/**/*"
28
+ ]
29
+ end
30
+ Jeweler::RubygemsDotOrgTasks.new
31
+
32
+ require 'rspec/core'
33
+ require 'rspec/core/rake_task'
34
+ RSpec::Core::RakeTask.new(:spec) do |spec|
35
+ spec.pattern = FileList['spec/**/*_spec.rb']
36
+ end
37
+
38
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
39
+ spec.pattern = 'spec/**/*_spec.rb'
40
+ spec.rcov = true
41
+ end
42
+
43
+ task :default => :spec
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
+ desc "Run RSpec with code coverage"
56
+ task :coverage do
57
+ `rake spec COVERAGE=true`
58
+ #`open coverage/index.html`
59
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,108 @@
1
+ module ApplicationHelper
2
+ # Usage:
3
+ # use in view with bootstrap
4
+ # = simple_model_details(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_model_details(User.first, attrs: ["email", "name"])
11
+ # Add created_at, updated_at:
12
+ # = simple_model_details(User.first, timestamps: true)
13
+ # Custom table class
14
+ # = simple_model_details(User.first, table_class: "table")
15
+ #
16
+ # Example:
17
+ #= raw simple_model_details(resource, attrs: ["name"], timestamps: true, table_class: "table")
18
+
19
+ def simple_model_details(m, options = { })
20
+ if options[:table_class]
21
+ table_class = options[:table_class]
22
+ else
23
+ table_class = "table table-striped table-bordered"
24
+ end
25
+
26
+ # column used to describe association
27
+ association_desc_keys = ["name", "email", "desc", "type", "id"]
28
+ special_attrs = ["id", "created_at", "updated_at"]
29
+ object_tableize = m.class.to_s.tableize.singularize
30
+
31
+ # foreign keys, beware! magic!
32
+ assoc_keys = m.class.reflect_on_all_associations.collect { |a| a.foreign_key }.uniq & m.attributes.keys
33
+ # associations classes
34
+ associations = Hash.new
35
+ assoc_keys.each do |ak|
36
+ associations[ak] = m.class.reflect_on_all_associations.select { |a| a.foreign_key == ak }.first.klass
37
+ end
38
+
39
+ if options[:attrs]
40
+ attrs = options[:attrs].collect { |k| k.to_s }
41
+ else
42
+ # remove special attributes
43
+ attrs = m.attributes.keys.sort
44
+ special_attrs.each do |s|
45
+ attrs.delete(s)
46
+ end
47
+ end
48
+
49
+ # model's regular attributes
50
+ details = Array.new
51
+ attrs.each do |d|
52
+ # 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" 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
+
67
+ if assoc_keys.include?(d)
68
+ r = associations[d].where(id: value.to_i).first
69
+ can_read = can?(:read, r)
70
+
71
+ # nobody wants id
72
+ value = ""
73
+ association_desc_keys.each do |ak|
74
+ if r.attributes[ak]
75
+ value = r.attributes[ak]
76
+ break
77
+ end
78
+ end
79
+
80
+ # if can read, link to it
81
+ if can_read
82
+ value = "<a href=\"#{url_for(r)}\">#{value}</a>"
83
+ end
84
+
85
+ end
86
+
87
+ #details << [, m.attributes[d]]
88
+ # http://apidock.com/rails/ActiveModel/Translation/human_attribute_name
89
+ details << [key, value]
90
+ end
91
+
92
+ # timestamps
93
+ if options[:timestamps]
94
+ #details << [m.class.human_attribute_name("created_at"), l(m.attributes["created_at"], format: :long)]
95
+ #details << [m.class.human_attribute_name("updated_at"), l(m.attributes["updated_at"], format: :long)]
96
+ details << [I18n.t("created_at"), l(m.attributes["created_at"], format: :long)]
97
+ details << [I18n.t("updated_at"), l(m.attributes["updated_at"], format: :long)]
98
+ end
99
+
100
+ s = "<table class=\"#{table_class}\">"
101
+ details.each do |d|
102
+ s += "<tr><td>#{d[0]}</td><td>#{d[1]}</td></tr>\n"
103
+ end
104
+ s += "</table>"
105
+
106
+ return s
107
+ end
108
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_show_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aleksander Kwiatkowski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-15 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &28397880 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.3.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *28397880
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &28395680 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *28395680
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &28395160 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.6.4
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *28395160
47
+ - !ruby/object:Gem::Dependency
48
+ name: simplecov
49
+ requirement: &28394620 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *28394620
58
+ description: Simple helper to create 'show' pages easily.
59
+ email: bobikx@poczta.fm
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files:
63
+ - LICENSE.txt
64
+ - README.md
65
+ files:
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - VERSION
72
+ - lib/simple_show_helper.rb
73
+ homepage: http://github.com/akwiatkowski/simple_show_helper
74
+ licenses:
75
+ - LGPLv3
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ segments:
87
+ - 0
88
+ hash: 587519105097511732
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 1.8.15
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Simple helper to create 'show' pages easily
101
+ test_files: []