material 0.2.13 → 0.2.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/material/base.rb +3 -0
- data/lib/material/concerns/attributes.rb +57 -0
- data/lib/material/concerns/core.rb +2 -0
- data/lib/material/concerns/format.rb +21 -5
- data/lib/material/version.rb +1 -1
- data/lib/material.rb +2 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5becf0ac24dac90be88a65b0e301857e730a808b7c2df45f2dbc05c541fb67b4
|
4
|
+
data.tar.gz: 2ae72ede1ae9ee8f2c617ff3d3b5e6f28f7d8627eca2e5436d691bc8f4963f09
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe93d86cf18b763c7b9eab3224e6ed4b3965e2450eb0ba928ee10063839d9c082946b95e5738f5adbb9bfa89564f04dd20eed839e30a0d856912da272f114242
|
7
|
+
data.tar.gz: a1f897b4683713ea9a90c6bbf80bd130a3a92c753eb814c841c4c7c5e56b4f791fa7df7d0e025f2002cc514f1b50242fb2cfd176570578f8adfc913e5f9b0dff
|
data/lib/material/base.rb
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
module Material
|
4
4
|
class Base < Spicerack::AttributeObject
|
5
|
+
extend ActiveSupport::NumberHelper
|
6
|
+
|
5
7
|
include Material::Components
|
6
8
|
include Material::Core
|
7
9
|
include Material::Display
|
@@ -9,6 +11,7 @@ module Material
|
|
9
11
|
include Material::Site
|
10
12
|
include Material::For
|
11
13
|
include Material::Format
|
14
|
+
include Material::Attributes
|
12
15
|
|
13
16
|
register_component :list_item_style
|
14
17
|
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Material
|
4
|
+
module Attributes
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
delegate :head_attributes, :tail_attributes, to: :class
|
9
|
+
|
10
|
+
memoize :attribute_values
|
11
|
+
memoize :attribute_types
|
12
|
+
memoize :formatted_attributes
|
13
|
+
memoize :sorted_attribute_names
|
14
|
+
memoize :relationship_attributes
|
15
|
+
end
|
16
|
+
|
17
|
+
class_methods do
|
18
|
+
def head_attributes
|
19
|
+
%w[id]
|
20
|
+
end
|
21
|
+
|
22
|
+
def tail_attributes
|
23
|
+
%w[created_at updated_at]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def human_attribute_value(attribute)
|
28
|
+
formatted_attributes.fetch(attribute)
|
29
|
+
end
|
30
|
+
|
31
|
+
def attribute_values
|
32
|
+
attribute_names.each_with_object({}) { |attribute, hash| hash[attribute] = public_send(attribute.to_sym) }
|
33
|
+
end
|
34
|
+
|
35
|
+
def attribute_types
|
36
|
+
attribute_names.each_with_object({}) { |attribute, hash| hash[attribute] = type_for_attribute(attribute) }
|
37
|
+
end
|
38
|
+
|
39
|
+
def formatted_attributes
|
40
|
+
attribute_types.each_with_object({}) do |(attribute, attribute_type), hash|
|
41
|
+
hash[attribute] = format_by_type(attribute_values[attribute], type: attribute_type)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def sorted_attribute_names
|
46
|
+
[
|
47
|
+
(head_attributes & attribute_names),
|
48
|
+
(attribute_names - head_attributes - tail_attributes).sort,
|
49
|
+
(tail_attributes & attribute_names),
|
50
|
+
].flatten
|
51
|
+
end
|
52
|
+
|
53
|
+
def relationship_attributes
|
54
|
+
source_class.reflect_on_all_associations.map(&:foreign_key)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -9,6 +9,8 @@ module Material
|
|
9
9
|
attr_reader :source
|
10
10
|
|
11
11
|
delegate :name, to: :class, prefix: true
|
12
|
+
delegate :class, to: :source, prefix: true
|
13
|
+
delegate :attribute_names, :human_attribute_name, :type_for_attribute, to: :source_class
|
12
14
|
|
13
15
|
delegate_missing_to :source
|
14
16
|
end
|
@@ -6,18 +6,34 @@ module Material
|
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
8
|
included do
|
9
|
-
delegate :format_date, :format_time, :
|
9
|
+
delegate :format_date, :format_time, :format_number, :format_by_type, to: :class
|
10
10
|
end
|
11
11
|
|
12
12
|
class_methods do
|
13
13
|
def format_date(date)
|
14
|
-
date.to_s(:long)
|
14
|
+
date.to_date.to_s(:long)
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
18
|
-
|
17
|
+
def format_time(time)
|
18
|
+
time.to_s(:long)
|
19
|
+
end
|
20
|
+
|
21
|
+
def format_number(number)
|
22
|
+
number_to_delimited(number)
|
23
|
+
end
|
24
|
+
|
25
|
+
def format_by_type(value, type:)
|
26
|
+
case type.to_sym
|
27
|
+
when :date
|
28
|
+
format_date(value)
|
29
|
+
when :datetime, :timestamp
|
30
|
+
format_time(value)
|
31
|
+
when :decimal, :float, :integer
|
32
|
+
format_number(value)
|
33
|
+
else
|
34
|
+
value
|
35
|
+
end
|
19
36
|
end
|
20
|
-
alias_method :format_time, :format_datetime
|
21
37
|
end
|
22
38
|
end
|
23
39
|
end
|
data/lib/material/version.rb
CHANGED
data/lib/material.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require "active_support"
|
4
4
|
require "active_support/core_ext"
|
5
|
+
require "active_support/number_helper"
|
5
6
|
|
6
7
|
require "spicerack"
|
7
8
|
|
@@ -15,6 +16,7 @@ require_relative "material/concerns/pagination"
|
|
15
16
|
require_relative "material/concerns/mount"
|
16
17
|
require_relative "material/concerns/collection"
|
17
18
|
require_relative "material/concerns/format"
|
19
|
+
require_relative "material/concerns/attributes"
|
18
20
|
|
19
21
|
require "material/version"
|
20
22
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: material
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Garside
|
@@ -208,6 +208,7 @@ files:
|
|
208
208
|
- README.md
|
209
209
|
- lib/material.rb
|
210
210
|
- lib/material/base.rb
|
211
|
+
- lib/material/concerns/attributes.rb
|
211
212
|
- lib/material/concerns/collection.rb
|
212
213
|
- lib/material/concerns/components.rb
|
213
214
|
- lib/material/concerns/core.rb
|