simple_attribute 0.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.
- checksums.yaml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +10 -0
- data/lib/simple_attribute.rb +32 -0
- data/lib/simple_attribute/attributes.rb +18 -0
- data/lib/simple_attribute/attributes/association.rb +28 -0
- data/lib/simple_attribute/attributes/avatar.rb +36 -0
- data/lib/simple_attribute/attributes/base.rb +114 -0
- data/lib/simple_attribute/attributes/boolean.rb +38 -0
- data/lib/simple_attribute/attributes/date.rb +20 -0
- data/lib/simple_attribute/attributes/datetime.rb +10 -0
- data/lib/simple_attribute/attributes/enumeration.rb +23 -0
- data/lib/simple_attribute/attributes/file.rb +19 -0
- data/lib/simple_attribute/attributes/image.rb +15 -0
- data/lib/simple_attribute/attributes/link.rb +28 -0
- data/lib/simple_attribute/attributes/video.rb +15 -0
- data/lib/simple_attribute/builder.rb +48 -0
- data/lib/simple_attribute/config.rb +5 -0
- data/lib/simple_attribute/helpers.rb +12 -0
- data/lib/simple_attribute/matcher.rb +42 -0
- data/lib/simple_attribute/version.rb +3 -0
- metadata +151 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7d7d27a575f2e75a680cb3ee8ab06a253e33c41c
|
4
|
+
data.tar.gz: ca05d02682f40efe436d6fc21f0c429a29bbaa53
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: df9b011f0f07efd670009fa2b23b74a41daaa87ab40e0fc25909d34c46b144995a5e8c8b9be31d6cfe6352f60c7ba2d81e7652a3b0730a43e9a3a9e0f54686ab
|
7
|
+
data.tar.gz: 40d43acd3e77581584e3f85f5ce7807a7d516b5034dd817a5f0abf311bd2e50e7e45e24165e98d48685e9b92d32c1ca6aa4e81e5e6725fc344322f95ac76d138
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at info@hardpixel.eu. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Jonian Guveli
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# SimpleAttribute
|
2
|
+
|
3
|
+
Attribute decorator support for ActiveRecord models.
|
4
|
+
|
5
|
+
[](https://badge.fury.io/rb/simple_attribute)
|
6
|
+
[](https://travis-ci.org/hardpixel/simple-attribute)
|
7
|
+
[](https://codeclimate.com/github/hardpixel/simple-attribute/maintainability)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'simple_attribute'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install simple_attribute
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
TODO: Write usage instructions here
|
28
|
+
|
29
|
+
## Development
|
30
|
+
|
31
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`.
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hardpixel/simple-attribute. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the ActiveContent project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hardpixel/simple-attribute/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'action_view'
|
3
|
+
require 'mini_mime'
|
4
|
+
require 'hashie'
|
5
|
+
require 'simple_attribute/version'
|
6
|
+
|
7
|
+
module SimpleAttribute
|
8
|
+
extend ActiveSupport::Autoload
|
9
|
+
|
10
|
+
# Autoload base modules
|
11
|
+
autoload :Config
|
12
|
+
autoload :Matcher
|
13
|
+
autoload :Builder
|
14
|
+
autoload :Attributes
|
15
|
+
autoload :Helpers
|
16
|
+
|
17
|
+
# Set attr accessors
|
18
|
+
mattr_accessor :config
|
19
|
+
|
20
|
+
# Set config options
|
21
|
+
@@config = Config.new
|
22
|
+
|
23
|
+
# Setup module
|
24
|
+
def self.setup
|
25
|
+
yield config
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Include action view helpers
|
30
|
+
if defined? ActionView::Base
|
31
|
+
ActionView::Base.send :include, SimpleAttribute::Helpers
|
32
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module SimpleAttribute
|
2
|
+
module Attributes
|
3
|
+
extend ActiveSupport::Autoload
|
4
|
+
|
5
|
+
# Autoload attribute decorators
|
6
|
+
autoload :Base
|
7
|
+
autoload :Avatar
|
8
|
+
autoload :Image
|
9
|
+
autoload :Video
|
10
|
+
autoload :Link
|
11
|
+
autoload :Date
|
12
|
+
autoload :Datetime
|
13
|
+
autoload :Boolean
|
14
|
+
autoload :Association
|
15
|
+
autoload :Enumeration
|
16
|
+
autoload :File
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module SimpleAttribute
|
2
|
+
module Attributes
|
3
|
+
class Association < Base
|
4
|
+
# Attribute name
|
5
|
+
def attribute_name
|
6
|
+
'association'
|
7
|
+
end
|
8
|
+
|
9
|
+
# Link label method
|
10
|
+
def label_method
|
11
|
+
@options.fetch(:label, :id)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Link title method
|
15
|
+
def title_method
|
16
|
+
@options.fetch(:title, :id)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Render attribute
|
20
|
+
def render_attribute
|
21
|
+
label = value.try(label_method)
|
22
|
+
title = value.try(title_method)
|
23
|
+
|
24
|
+
link_to label, value, html_options.merge(title: title)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module SimpleAttribute
|
2
|
+
module Attributes
|
3
|
+
class Avatar < Base
|
4
|
+
# Attribute name
|
5
|
+
def attribute_name
|
6
|
+
'avatar'
|
7
|
+
end
|
8
|
+
|
9
|
+
# Image size
|
10
|
+
def size
|
11
|
+
options.fetch :size, 19
|
12
|
+
end
|
13
|
+
|
14
|
+
# Get gravatar
|
15
|
+
def gravatar
|
16
|
+
hash = Digest::MD5::hexdigest(value)
|
17
|
+
"https://www.gravatar.com/avatar/#{hash}?rating=PG&size=#{size}&default=mm"
|
18
|
+
end
|
19
|
+
|
20
|
+
# Get custom avatar
|
21
|
+
def custom_avatar
|
22
|
+
attribute = options.fetch :avatar, nil
|
23
|
+
record.try attribute if attribute
|
24
|
+
end
|
25
|
+
|
26
|
+
# Render attribute
|
27
|
+
def render_attribute
|
28
|
+
avatar = custom_avatar || gravatar
|
29
|
+
classes = "#{html_options[:class]} avatar-image".strip
|
30
|
+
options = html_options.merge({ class: classes, width: size, height: size })
|
31
|
+
|
32
|
+
image_tag avatar, options
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
module SimpleAttribute
|
2
|
+
module Attributes
|
3
|
+
class Base
|
4
|
+
attr_accessor :options, :record, :attribute, :value
|
5
|
+
|
6
|
+
# Initialize base attribute
|
7
|
+
def initialize(context, options)
|
8
|
+
@context = context
|
9
|
+
@options = options.reverse_merge defaults
|
10
|
+
@record = options.fetch :record
|
11
|
+
@attribute = options.fetch :attribute
|
12
|
+
@value = @record.send attribute if attribute
|
13
|
+
end
|
14
|
+
|
15
|
+
# Get default options
|
16
|
+
def defaults
|
17
|
+
Hash(SimpleAttribute.config.send(:"#{renderer_name}")).symbolize_keys
|
18
|
+
end
|
19
|
+
|
20
|
+
# Check if has value
|
21
|
+
def value?
|
22
|
+
if value.is_a? ActiveRecord::Base
|
23
|
+
value.try(:id).present?
|
24
|
+
elsif value.is_a? String
|
25
|
+
strip_tags(value).present?
|
26
|
+
else
|
27
|
+
value.present?
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Get default value
|
32
|
+
def default_value
|
33
|
+
@options.fetch :default_value, '—'
|
34
|
+
end
|
35
|
+
|
36
|
+
# Check if needs wrapper
|
37
|
+
def wrapper?
|
38
|
+
options[:wrapper] != false
|
39
|
+
end
|
40
|
+
|
41
|
+
# Get wrapper
|
42
|
+
def wrapper
|
43
|
+
wrapper = options.fetch :wrapper, nil
|
44
|
+
wrapper = SimpleAttribute.config.wrappers.try(:"#{wrapper}") unless wrapper.is_a? Hash
|
45
|
+
|
46
|
+
Hash(wrapper).symbolize_keys
|
47
|
+
end
|
48
|
+
|
49
|
+
# Wrapper name
|
50
|
+
def renderer_name
|
51
|
+
name = self.class.name.gsub('Attribute', '')
|
52
|
+
name = name.demodulize.underscore
|
53
|
+
|
54
|
+
name == 'base' ? 'string' : "#{name}".dasherize
|
55
|
+
end
|
56
|
+
|
57
|
+
# Attribute name
|
58
|
+
def attribute_name
|
59
|
+
"#{attribute}".dasherize
|
60
|
+
end
|
61
|
+
|
62
|
+
# Attribute html options
|
63
|
+
def html_options
|
64
|
+
options.fetch :html, {}
|
65
|
+
end
|
66
|
+
|
67
|
+
# Wrapper html
|
68
|
+
def wrapper_html
|
69
|
+
classes = ['attribute', attribute_name, renderer_name].uniq.join ' '
|
70
|
+
classes = "#{wrapper[:class]} #{classes}".strip
|
71
|
+
|
72
|
+
wrapper.merge(class: classes)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Render default value
|
76
|
+
def render_default_value
|
77
|
+
if default_value.present?
|
78
|
+
@options[:wrapper] = nil
|
79
|
+
content_tag :span, default_value, class: "attribute-default-value"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Render attribute
|
84
|
+
def render_attribute
|
85
|
+
"#{value || default_value}".html_safe
|
86
|
+
end
|
87
|
+
|
88
|
+
# Render attribute or default
|
89
|
+
def render_with_default
|
90
|
+
if value?
|
91
|
+
render_attribute
|
92
|
+
else
|
93
|
+
render_default_value
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Render wrapper
|
98
|
+
def render_wrapper
|
99
|
+
content_tag :span, render_with_default.to_s.html_safe, wrapper_html
|
100
|
+
end
|
101
|
+
|
102
|
+
# Render
|
103
|
+
def render
|
104
|
+
content = wrapper? ? render_wrapper : render_with_default
|
105
|
+
content.to_s.html_safe
|
106
|
+
end
|
107
|
+
|
108
|
+
# Use view helpers if method is missing
|
109
|
+
def method_missing(method, *args, &block)
|
110
|
+
@context.respond_to?(method) ? @context.send(method, *args, &block) : super
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module SimpleAttribute
|
2
|
+
module Attributes
|
3
|
+
class Boolean < Base
|
4
|
+
# Invert classes
|
5
|
+
def invert_classes?
|
6
|
+
options[:invert] == true
|
7
|
+
end
|
8
|
+
|
9
|
+
# True class
|
10
|
+
def true_class
|
11
|
+
invert_classes? ? options[:false] : options[:true]
|
12
|
+
end
|
13
|
+
|
14
|
+
# False class
|
15
|
+
def false_class
|
16
|
+
invert_classes? ? options[:true] : options[:false]
|
17
|
+
end
|
18
|
+
|
19
|
+
# Wrapper html
|
20
|
+
def wrapper_html
|
21
|
+
classes = value? ? true_class : false_class
|
22
|
+
classes = "#{super[:class]} #{classes}".strip
|
23
|
+
|
24
|
+
super.merge(class: classes)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Render default attribute
|
28
|
+
def render_default_value
|
29
|
+
'No'
|
30
|
+
end
|
31
|
+
|
32
|
+
# Render attribute
|
33
|
+
def render_attribute
|
34
|
+
'Yes'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module SimpleAttribute
|
2
|
+
module Attributes
|
3
|
+
class Date < Base
|
4
|
+
# Date default format
|
5
|
+
def default_format
|
6
|
+
'<date>%d %b %Y</date>'
|
7
|
+
end
|
8
|
+
|
9
|
+
# Date format
|
10
|
+
def date_format
|
11
|
+
options.fetch :format, default_format
|
12
|
+
end
|
13
|
+
|
14
|
+
# Render attribute
|
15
|
+
def render_attribute
|
16
|
+
value.try :strftime, date_format
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module SimpleAttribute
|
2
|
+
module Attributes
|
3
|
+
class Enumeration < Base
|
4
|
+
# Attribute name
|
5
|
+
def attribute_name
|
6
|
+
'enumeration'
|
7
|
+
end
|
8
|
+
|
9
|
+
# Link label method
|
10
|
+
def label_method
|
11
|
+
@options.fetch(:label, :titleize)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Render attribute
|
15
|
+
def render
|
16
|
+
values = @value.to_a.map(&:"#{label_method}")
|
17
|
+
items = values.map { |item| @value = item; super }
|
18
|
+
|
19
|
+
items.join.html_safe
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module SimpleAttribute
|
2
|
+
module Attributes
|
3
|
+
class File < Link
|
4
|
+
# Attribute name
|
5
|
+
def attribute_name
|
6
|
+
'file'
|
7
|
+
end
|
8
|
+
|
9
|
+
# Render attribute
|
10
|
+
def render_attribute
|
11
|
+
file = value.to_s.split('/').last.split('.').first
|
12
|
+
label = file.try(label_method)
|
13
|
+
title = file.try(title_method)
|
14
|
+
|
15
|
+
link_to label, value.to_s, html_options.merge(title: title)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module SimpleAttribute
|
2
|
+
module Attributes
|
3
|
+
class Link < Base
|
4
|
+
# Attribute name
|
5
|
+
def attribute_name
|
6
|
+
'link'
|
7
|
+
end
|
8
|
+
|
9
|
+
# Link label method
|
10
|
+
def label_method
|
11
|
+
@options.fetch(:label, :titleize)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Link title method
|
15
|
+
def title_method
|
16
|
+
@options.fetch(:title, :titleize)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Render attribute
|
20
|
+
def render_attribute
|
21
|
+
label = value.try(label_method)
|
22
|
+
title = value.try(title_method)
|
23
|
+
|
24
|
+
link_to label, value.to_s, html_options.merge(title: title)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module SimpleAttribute
|
2
|
+
class Builder
|
3
|
+
# Initialize base attribute
|
4
|
+
def initialize(context, options={})
|
5
|
+
@context = context
|
6
|
+
@options = options
|
7
|
+
@renderer = options.fetch :as, guess_renderer
|
8
|
+
end
|
9
|
+
|
10
|
+
# Base attribute renderer
|
11
|
+
def base_renderer
|
12
|
+
'SimpleAttribute::Attributes::Base'.safe_constantize
|
13
|
+
end
|
14
|
+
|
15
|
+
# Guess renderer
|
16
|
+
def guess_renderer
|
17
|
+
record = @options[:record]
|
18
|
+
attrib = @options[:attribute]
|
19
|
+
|
20
|
+
SimpleAttribute::Matcher.new(record, attrib).match
|
21
|
+
end
|
22
|
+
|
23
|
+
# Find attribute renderer
|
24
|
+
def find_renderer(renderer)
|
25
|
+
renderer = "#{renderer}".classify
|
26
|
+
custom = "#{renderer}Attribute".safe_constantize
|
27
|
+
builtin = "SimpleAttribute::Attributes::#{renderer}".safe_constantize if custom.nil?
|
28
|
+
|
29
|
+
custom || builtin || base_renderer
|
30
|
+
end
|
31
|
+
|
32
|
+
# Render attribute
|
33
|
+
def render_attribute(renderer)
|
34
|
+
renderer = find_renderer renderer
|
35
|
+
options = @options.except(:as)
|
36
|
+
|
37
|
+
renderer.new(@context, options).render
|
38
|
+
end
|
39
|
+
|
40
|
+
# Render
|
41
|
+
def render
|
42
|
+
@rendered_attribute ||= begin
|
43
|
+
html = Array(@renderer).map { |w| render_attribute(w) }
|
44
|
+
html.reject(&:blank?).join.html_safe
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module SimpleAttribute
|
2
|
+
module Helpers
|
3
|
+
# Render simple attribute
|
4
|
+
def simple_attribute_for(record, attribute, options={})
|
5
|
+
options = options.merge(record: record, attribute: attribute)
|
6
|
+
SimpleAttribute::Builder.new(self, options).render
|
7
|
+
end
|
8
|
+
|
9
|
+
# Alias helper method
|
10
|
+
alias :attribute_for :simple_attribute_for
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module SimpleAttribute
|
2
|
+
class Matcher
|
3
|
+
# Initialize attribute matcher
|
4
|
+
def initialize(record, attribute)
|
5
|
+
@record = record
|
6
|
+
@attribute = attribute
|
7
|
+
@value = record.try(attribute)
|
8
|
+
|
9
|
+
find_type
|
10
|
+
find_type_name
|
11
|
+
end
|
12
|
+
|
13
|
+
# Find attribute type
|
14
|
+
def find_type
|
15
|
+
@type ||= @record.class.attribute_types["#{@attribute}"]
|
16
|
+
end
|
17
|
+
|
18
|
+
# Find type name
|
19
|
+
def find_type_name
|
20
|
+
@type_name ||= @type.class.name.demodulize.downcase unless @type.nil?
|
21
|
+
end
|
22
|
+
|
23
|
+
# Check if file method
|
24
|
+
def file_method?
|
25
|
+
methods = [:mounted_as, :file?, :public_filename]
|
26
|
+
methods.any? { |m| @value.respond_to?(m) }
|
27
|
+
end
|
28
|
+
|
29
|
+
# Find file type
|
30
|
+
def find_file_type
|
31
|
+
media_type = MiniMime.lookup_by_filename("#{@value}").content_type.split('/').first
|
32
|
+
available = [:image, :video]
|
33
|
+
|
34
|
+
available.find { |t| "#{t}" == media_type } || :file
|
35
|
+
end
|
36
|
+
|
37
|
+
# Get renderer match
|
38
|
+
def match
|
39
|
+
file_method? ? find_file_type : @type_name.to_sym
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_attribute
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonian Guveli
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionview
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: hashie
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.5'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mini_mime
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.14'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.14'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: minitest
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '5.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '5.0'
|
97
|
+
description: Attribute decorator support for ActiveRecord models.
|
98
|
+
email:
|
99
|
+
- jonian@hardpixel.eu
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- CODE_OF_CONDUCT.md
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- lib/simple_attribute.rb
|
110
|
+
- lib/simple_attribute/attributes.rb
|
111
|
+
- lib/simple_attribute/attributes/association.rb
|
112
|
+
- lib/simple_attribute/attributes/avatar.rb
|
113
|
+
- lib/simple_attribute/attributes/base.rb
|
114
|
+
- lib/simple_attribute/attributes/boolean.rb
|
115
|
+
- lib/simple_attribute/attributes/date.rb
|
116
|
+
- lib/simple_attribute/attributes/datetime.rb
|
117
|
+
- lib/simple_attribute/attributes/enumeration.rb
|
118
|
+
- lib/simple_attribute/attributes/file.rb
|
119
|
+
- lib/simple_attribute/attributes/image.rb
|
120
|
+
- lib/simple_attribute/attributes/link.rb
|
121
|
+
- lib/simple_attribute/attributes/video.rb
|
122
|
+
- lib/simple_attribute/builder.rb
|
123
|
+
- lib/simple_attribute/config.rb
|
124
|
+
- lib/simple_attribute/helpers.rb
|
125
|
+
- lib/simple_attribute/matcher.rb
|
126
|
+
- lib/simple_attribute/version.rb
|
127
|
+
homepage: https://github.com/hardpixel/simple-attribute
|
128
|
+
licenses:
|
129
|
+
- MIT
|
130
|
+
metadata: {}
|
131
|
+
post_install_message:
|
132
|
+
rdoc_options: []
|
133
|
+
require_paths:
|
134
|
+
- lib
|
135
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
requirements: []
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 2.6.13
|
148
|
+
signing_key:
|
149
|
+
specification_version: 4
|
150
|
+
summary: Decorate ActiveRecord model attributes
|
151
|
+
test_files: []
|