simple_attribute 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a082dfa060ec25843eff594cba7d820dec86b19c9f590339cf386f9b0699d2e
4
- data.tar.gz: b84f8bbe82b372738feb813dd4e780ad72357d2edb79e827bc6c58e00c573aff
3
+ metadata.gz: 5e2bea8fc4b9aa3a9bc8d4d090e8159fb2dcb099a19a5fe3020e3ace4a1a235a
4
+ data.tar.gz: 1a696fba8b605b1d99ae5590b4d6b18f434788f2cc445e61a98a7a8bb2983ea5
5
5
  SHA512:
6
- metadata.gz: a0ae165e03e44acb4cbf25f0e96ee0d3f1a8e8d8312182e0ce02118c6296d33f09ec6ba62ee4878630499708ba5a31da939217bcfd51f88aafb033831770b7b7
7
- data.tar.gz: ed4f62a387b2db5ab4ef3d73bc4a702fc2d146385e96d7a8d2ac7b9ca649e5dd90948aa73c4c3d9eb33af1a052e92d481773cb2547a1ad63efa559c8afffeb01
6
+ metadata.gz: b07a696a31637e2cdc2f789af1b72fbd60f5a828c5558b89087dc67359d9af81c1e7be845faad5ea7251ffa58541b2aefa877eb43dfcc901265d86fc1ebf75ca
7
+ data.tar.gz: 96f58cf74c55583056d4970be94f0bc1682771c6e32bf3ab880f1a0cbd4bb7a07ebf2fe26816a7cda8901fef30fffdefe67bf1ef9815c3d33a2c67a06d1edeaa
data/README.md CHANGED
@@ -32,12 +32,8 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
32
32
 
33
33
  ## Contributing
34
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.
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hardpixel/simple-attribute.
36
36
 
37
37
  ## License
38
38
 
39
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 SimpleAttribute 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).
@@ -7,26 +7,21 @@ require 'simple_attribute/version'
7
7
  module SimpleAttribute
8
8
  extend ActiveSupport::Autoload
9
9
 
10
- # Autoload base modules
11
10
  autoload :Config
12
11
  autoload :Matcher
13
12
  autoload :Builder
14
13
  autoload :Attributes
15
14
  autoload :Helpers
16
15
 
17
- # Set attr accessors
18
16
  mattr_accessor :config
19
17
 
20
- # Set config options
21
18
  @@config = Config.new
22
19
 
23
- # Setup module
24
20
  def self.setup
25
21
  yield config
26
22
  end
27
23
  end
28
24
 
29
- # Include action view helpers
30
25
  if defined? ActionView::Base
31
26
  ActionView::Base.send :include, SimpleAttribute::Helpers
32
27
  end
@@ -2,7 +2,6 @@ module SimpleAttribute
2
2
  module Attributes
3
3
  extend ActiveSupport::Autoload
4
4
 
5
- # Autoload attribute decorators
6
5
  autoload :Base
7
6
  autoload :Avatar
8
7
  autoload :Image
@@ -1,27 +1,22 @@
1
1
  module SimpleAttribute
2
2
  module Attributes
3
3
  class Association < Base
4
- # Attribute name
5
4
  def attribute_name
6
5
  'association'
7
6
  end
8
7
 
9
- # Link label method
10
8
  def label_method
11
9
  @options.fetch(:label, :id)
12
10
  end
13
11
 
14
- # Link title method
15
12
  def title_method
16
13
  @options.fetch(:title, :id)
17
14
  end
18
15
 
19
- # Link url method
20
16
  def url_method
21
17
  @options.fetch(:url, nil)
22
18
  end
23
19
 
24
- # Render attribute
25
20
  def render_attribute
26
21
  label = value.try(label_method)
27
22
  title = value.try(title_method)
@@ -1,29 +1,24 @@
1
1
  module SimpleAttribute
2
2
  module Attributes
3
3
  class Avatar < Base
4
- # Attribute name
5
4
  def attribute_name
6
5
  'avatar'
7
6
  end
8
7
 
9
- # Image size
10
8
  def size
11
9
  options.fetch :size, 19
12
10
  end
13
11
 
14
- # Get gravatar
15
12
  def gravatar
16
13
  hash = Digest::MD5::hexdigest(value)
17
14
  "https://www.gravatar.com/avatar/#{hash}?rating=PG&size=#{size}&default=mm"
18
15
  end
19
16
 
20
- # Get custom avatar
21
17
  def custom_avatar
22
18
  attribute = options.fetch :avatar, nil
23
19
  record.try attribute if attribute
24
20
  end
25
21
 
26
- # Render attribute
27
22
  def render_attribute
28
23
  avatar = custom_avatar || gravatar
29
24
  classes = "#{html_options[:class]} avatar-image".strip
@@ -3,7 +3,6 @@ module SimpleAttribute
3
3
  class Base
4
4
  attr_accessor :options, :record, :attribute, :value
5
5
 
6
- # Initialize base attribute
7
6
  def initialize(context, options)
8
7
  @context = context
9
8
  @options = options.reverse_merge defaults
@@ -12,12 +11,10 @@ module SimpleAttribute
12
11
  @value = @record.send attribute if attribute
13
12
  end
14
13
 
15
- # Get default options
16
14
  def defaults
17
15
  Hash(SimpleAttribute.config.send(:"#{renderer_name}")).symbolize_keys
18
16
  end
19
17
 
20
- # Check if has value
21
18
  def value?
22
19
  if value.is_a? ActiveRecord::Base
23
20
  value.try(:id).present?
@@ -28,17 +25,14 @@ module SimpleAttribute
28
25
  end
29
26
  end
30
27
 
31
- # Get default value
32
28
  def default_value
33
29
  @options.fetch :default_value, '—'
34
30
  end
35
31
 
36
- # Check if needs wrapper
37
32
  def wrapper?
38
33
  options[:wrapper] != false
39
34
  end
40
35
 
41
- # Get wrapper
42
36
  def wrapper
43
37
  wrapper = options.fetch :wrapper, nil
44
38
  wrapper = SimpleAttribute.config.wrappers.try(:"#{wrapper}") unless wrapper.is_a? Hash
@@ -46,30 +40,25 @@ module SimpleAttribute
46
40
  Hash(wrapper).symbolize_keys
47
41
  end
48
42
 
49
- # Wrapper name
50
43
  def renderer_name
51
44
  name = self.class.name.gsub('Attribute', '')
52
45
  name = name.demodulize.underscore
53
46
 
54
- name == 'base' ? 'string' : "#{name}".dasherize
47
+ name == 'base' ? 'string' : name.to_s.dasherize
55
48
  end
56
49
 
57
- # Attribute name
58
50
  def attribute_name
59
- "#{attribute}".dasherize
51
+ attribute.to_s.dasherize
60
52
  end
61
53
 
62
- # Attribute label method
63
54
  def label_method
64
55
  @options.fetch(:label, :to_s)
65
56
  end
66
57
 
67
- # Attribute html options
68
58
  def html_options
69
59
  options.fetch :html, {}
70
60
  end
71
61
 
72
- # Wrapper html
73
62
  def wrapper_html
74
63
  classes = ['attribute', attribute_name, renderer_name].uniq.join ' '
75
64
  classes = "#{wrapper[:class]} #{classes}".strip
@@ -77,20 +66,17 @@ module SimpleAttribute
77
66
  wrapper.merge(class: classes)
78
67
  end
79
68
 
80
- # Render default value
81
69
  def render_default_value
82
- if default_value.present?
83
- @options[:wrapper] = nil
84
- content_tag :span, default_value, class: "attribute-default-value"
85
- end
70
+ return if default_value.blank?
71
+
72
+ @options[:wrapper] = nil
73
+ content_tag :span, default_value, class: 'attribute-default-value'
86
74
  end
87
75
 
88
- # Render attribute
89
76
  def render_attribute
90
- "#{value.try(label_method)}".html_safe
77
+ value.try(label_method).to_s.html_safe
91
78
  end
92
79
 
93
- # Render attribute or default
94
80
  def render_with_default
95
81
  if value?
96
82
  render_attribute
@@ -99,18 +85,15 @@ module SimpleAttribute
99
85
  end
100
86
  end
101
87
 
102
- # Render wrapper
103
88
  def render_wrapper
104
89
  content_tag :span, render_with_default.to_s.html_safe, wrapper_html
105
90
  end
106
91
 
107
- # Render
108
92
  def render
109
93
  content = wrapper? ? render_wrapper : render_with_default
110
94
  content.to_s.html_safe
111
95
  end
112
96
 
113
- # Use view helpers if method is missing
114
97
  def method_missing(method, *args, &block)
115
98
  @context.respond_to?(method) ? @context.send(method, *args, &block) : super
116
99
  end
@@ -1,22 +1,18 @@
1
1
  module SimpleAttribute
2
2
  module Attributes
3
3
  class Boolean < Base
4
- # Invert classes
5
4
  def invert_classes?
6
5
  options[:invert] == true
7
6
  end
8
7
 
9
- # True class
10
8
  def true_class
11
9
  invert_classes? ? options[:false] : options[:true]
12
10
  end
13
11
 
14
- # False class
15
12
  def false_class
16
13
  invert_classes? ? options[:true] : options[:false]
17
14
  end
18
15
 
19
- # Wrapper html
20
16
  def wrapper_html
21
17
  classes = value? ? true_class : false_class
22
18
  classes = "#{super[:class]} #{classes}".strip
@@ -24,12 +20,10 @@ module SimpleAttribute
24
20
  super.merge(class: classes)
25
21
  end
26
22
 
27
- # Render default attribute
28
23
  def render_default_value
29
24
  'No'
30
25
  end
31
26
 
32
- # Render attribute
33
27
  def render_attribute
34
28
  'Yes'
35
29
  end
@@ -1,17 +1,14 @@
1
1
  module SimpleAttribute
2
2
  module Attributes
3
3
  class Date < Base
4
- # Date default format
5
4
  def default_format
6
5
  '<date>%d %b %Y</date>'
7
6
  end
8
7
 
9
- # Date format
10
8
  def date_format
11
9
  options.fetch :format, default_format
12
10
  end
13
11
 
14
- # Render attribute
15
12
  def render_attribute
16
13
  value.try :strftime, date_format
17
14
  end
@@ -1,7 +1,6 @@
1
1
  module SimpleAttribute
2
2
  module Attributes
3
3
  class Datetime < Date
4
- # Date default format
5
4
  def default_format
6
5
  '<date>%d %b %Y</date> <time>%H:%M</time>'
7
6
  end
@@ -1,17 +1,14 @@
1
1
  module SimpleAttribute
2
2
  module Attributes
3
3
  class Enumeration < Base
4
- # Attribute name
5
4
  def attribute_name
6
5
  'enumeration'
7
6
  end
8
7
 
9
- # Link label method
10
8
  def label_method
11
9
  @options.fetch(:label, :titleize)
12
10
  end
13
11
 
14
- # Render attribute
15
12
  def render
16
13
  values = @value.to_a.map(&:"#{label_method}")
17
14
  items = values.map { |item| @value = item; super }
@@ -1,12 +1,10 @@
1
1
  module SimpleAttribute
2
2
  module Attributes
3
3
  class File < Link
4
- # Attribute name
5
4
  def attribute_name
6
5
  'file'
7
6
  end
8
7
 
9
- # Render attribute
10
8
  def render_attribute
11
9
  file = value.to_s.split('/').last.split('.').first
12
10
  label = file.try(label_method)
@@ -1,27 +1,24 @@
1
1
  module SimpleAttribute
2
2
  module Attributes
3
3
  class Image < Base
4
- # Attribute name
5
4
  def attribute_name
6
5
  'image'
7
6
  end
8
7
 
9
- # Render default value
10
- def render_default_value
8
+ def media_type
11
9
  @media_type ||= begin
12
- media_type = MiniMime.lookup_by_filename("#{default_value}")
13
- media_type.content_type.to_s.split('/').first
10
+ type = MiniMime.lookup_by_filename(default_value.to_s)
11
+ type.content_type.to_s.split('/').first
14
12
  end
13
+ end
15
14
 
16
- if 'image'.in?("#{@media_type}")
17
- default_url = asset_url(default_value)
18
- content_tag :img, nil, html_options.reverse_merge(src: default_url)
19
- else
20
- super
21
- end
15
+ def render_default_value
16
+ return super unless 'image'.in?(media_type.to_s)
17
+
18
+ default_url = asset_url(default_value)
19
+ content_tag :img, nil, html_options.reverse_merge(src: default_url)
22
20
  end
23
21
 
24
- # Render attribute
25
22
  def render_attribute
26
23
  content_tag :img, nil, html_options.reverse_merge(src: value)
27
24
  end
@@ -1,22 +1,18 @@
1
1
  module SimpleAttribute
2
2
  module Attributes
3
3
  class Link < Base
4
- # Attribute name
5
4
  def attribute_name
6
5
  'link'
7
6
  end
8
7
 
9
- # Link label method
10
8
  def label_method
11
9
  @options.fetch(:label, :titleize)
12
10
  end
13
11
 
14
- # Link title method
15
12
  def title_method
16
13
  @options.fetch(:title, :titleize)
17
14
  end
18
15
 
19
- # Render attribute
20
16
  def render_attribute
21
17
  label = value.try(label_method)
22
18
  title = value.try(title_method)
@@ -1,7 +1,6 @@
1
1
  module SimpleAttribute
2
2
  module Attributes
3
3
  class Money < Base
4
- # Render attribute
5
4
  def render_attribute
6
5
  value.try :format
7
6
  end
@@ -1,12 +1,10 @@
1
1
  module SimpleAttribute
2
2
  module Attributes
3
3
  class Video < Base
4
- # Attribute name
5
4
  def attribute_name
6
5
  'video'
7
6
  end
8
7
 
9
- # Render attribute
10
8
  def render_attribute
11
9
  content_tag :video, nil, html_options.reverse_merge(src: value)
12
10
  end
@@ -1,18 +1,15 @@
1
1
  module SimpleAttribute
2
2
  class Builder
3
- # Initialize base attribute
4
- def initialize(context, options={})
3
+ def initialize(context, options = {})
5
4
  @context = context
6
5
  @options = options
7
6
  @renderer = options.fetch :as, guess_renderer
8
7
  end
9
8
 
10
- # Base attribute renderer
11
9
  def base_renderer
12
10
  'SimpleAttribute::Attributes::Base'.safe_constantize
13
11
  end
14
12
 
15
- # Guess renderer
16
13
  def guess_renderer
17
14
  record = @options[:record]
18
15
  attrib = @options[:attribute]
@@ -20,7 +17,6 @@ module SimpleAttribute
20
17
  SimpleAttribute::Matcher.new(record, attrib).match
21
18
  end
22
19
 
23
- # Find attribute renderer
24
20
  def find_renderer(renderer)
25
21
  namespace = @options[:namespace]
26
22
  renderer = "#{renderer}".classify
@@ -31,7 +27,6 @@ module SimpleAttribute
31
27
  custom || builtin || base_renderer
32
28
  end
33
29
 
34
- # Render attribute
35
30
  def render_attribute(renderer)
36
31
  renderer = find_renderer renderer
37
32
  options = @options.except(:as)
@@ -39,7 +34,6 @@ module SimpleAttribute
39
34
  renderer.new(@context, options).render
40
35
  end
41
36
 
42
- # Render
43
37
  def render
44
38
  @rendered_attribute ||= begin
45
39
  html = Array(@renderer).map { |w| render_attribute(w) }
@@ -1,12 +1,10 @@
1
1
  module SimpleAttribute
2
2
  module Helpers
3
- # Render simple attribute
4
- def simple_attribute_for(record, attribute, options={})
3
+ def simple_attribute_for(record, attribute, options = {})
5
4
  options = options.merge(record: record, attribute: attribute)
6
5
  SimpleAttribute::Builder.new(self, options).render
7
6
  end
8
7
 
9
- # Alias helper method
10
8
  alias :attribute_for :simple_attribute_for
11
9
  end
12
10
  end
@@ -1,6 +1,5 @@
1
1
  module SimpleAttribute
2
2
  class Matcher
3
- # Initialize attribute matcher
4
3
  def initialize(record, attribute)
5
4
  @record = record
6
5
  @attribute = attribute
@@ -10,32 +9,27 @@ module SimpleAttribute
10
9
  find_type_name
11
10
  end
12
11
 
13
- # Find attribute type
14
12
  def find_type
15
- @type ||= @record.class.attribute_types["#{@attribute}"]
13
+ @type ||= @record.class.attribute_types[@attribute.to_s]
16
14
  end
17
15
 
18
- # Find type name
19
16
  def find_type_name
20
17
  @type_name ||= @type.class.name.demodulize.downcase unless @type.nil?
21
18
  end
22
19
 
23
- # Check if file method
24
20
  def file_method?
25
21
  methods = [:mounted_as, :file?, :public_filename]
26
22
  methods.any? { |m| @value.respond_to?(m) }
27
23
  end
28
24
 
29
- # Find file type
30
25
  def find_file_type
31
- media_type = MiniMime.lookup_by_filename("#{@value}")
26
+ media_type = MiniMime.lookup_by_filename(@value.to_s)
32
27
  media_type = media_type.content_type.split('/').first unless media_type.nil?
33
28
  available = [:image, :video]
34
29
 
35
- available.find { |t| "#{t}" == media_type } || :file
30
+ available.find { |t| t.to_s == media_type } || :file
36
31
  end
37
32
 
38
- # Get renderer match
39
33
  def match
40
34
  file_method? ? find_file_type : @type_name.to_sym
41
35
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleAttribute
2
- VERSION = '0.1.5'
2
+ VERSION = '0.1.6'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_attribute
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonian Guveli
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-23 00:00:00.000000000 Z
11
+ date: 2019-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionview
@@ -101,7 +101,6 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - CODE_OF_CONDUCT.md
105
104
  - Gemfile
106
105
  - LICENSE.txt
107
106
  - README.md
@@ -1,74 +0,0 @@
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/