morse_assetable 0.2.1 → 1.0.0

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
  SHA1:
3
- metadata.gz: 30bc73733a56797cee4aa0d0191d4a9c02996c94
4
- data.tar.gz: 2c9399be95617829dd16c2bf6a67497af44ed5c5
3
+ metadata.gz: cf85029cd3597c4c7c633860ca9e489081ea2a7f
4
+ data.tar.gz: a08b4e73e0a6a50e7e1bb04c709c575c2fec2976
5
5
  SHA512:
6
- metadata.gz: f31b54cffe180e50ea6fbf1ad3ea7e6969bd8717f3c1dc8203a39344c51c0bb2884d1acbaf301e2806637354dd5e86d7ecddbc6fc06a534055437999c22a87c5
7
- data.tar.gz: 5ba13a76528ca25efbee61f80ac10ed5956a3e45f00af92c15345dc80366073ac30aa250c88669f96f5ec02fd9a3bd7258cfac12d11b88cb8684c170c93af429
6
+ metadata.gz: 2e5ba0fd0dece34252779e57f0cb7d6aeec49228d35945ccd35f1048f3840b9602b91f89e22fb2b4de792e0e71884eaa9e266322d2501c7eb403c0ed5be11351
7
+ data.tar.gz: b378d37a3be204c2fd30fc7e174a84bbab3c945653fa4328ec2b7b798b4a8e8447796c51dfbef778e7127e939aa606d35bc1b70dd74bc9f49001563bce25d627
@@ -1,180 +1,182 @@
1
+ # MorseAssetable
1
2
  require "morse_assetable/version"
2
- module MorseAssetableViewHelpers
3
- def asset_input(f, i, bn = nil)
4
- return multiple_assets(i, f) unless bn
5
- asset = i.send(bn)
6
- render partial: 'cms/assets/partials/asset',
7
- locals: { asset: asset,
8
- base_name: bn,
9
- f: f }
10
- end
3
+ module MorseAssetable # rubocop:disable Metrics/ModuleLength
4
+ module ViewHelpers
5
+ def asset_input(f, i, bn = nil)
6
+ return multiple_assets(i, f) unless bn
7
+ asset = i.send(bn)
8
+ render partial: 'cms/assets/partials/asset',
9
+ locals: { asset: asset,
10
+ base_name: bn,
11
+ f: f }
12
+ end
11
13
 
12
- def multiple_assets(i, f)
13
- render partial: 'cms/assets/partials/assets',
14
- locals: { assets: i.assets3,
15
- f: f }
16
- end
14
+ def multiple_assets(i, f)
15
+ render partial: 'cms/assets/partials/assets',
16
+ locals: { assets: i.assets,
17
+ f: f }
18
+ end
17
19
 
18
- def nice_asset(a, version = nil)
19
- return unless a && a.attachment
20
- image_url = a.attachment.url
21
- image_url = a.attachment.versions[version].url if version
22
- image_tag image_url, alt: a.alt
20
+ def nice_asset(a, version = nil)
21
+ return unless a && a.attachment
22
+ image_url = a.attachment.url
23
+ image_url = a.attachment.versions[version].url if version
24
+ image_tag image_url, alt: a.alt
25
+ end
23
26
  end
24
- end
25
- # MorseAssetable
26
- module MorseAssetable # rubocop:disable Metrics/ModuleLength
27
- extend ActiveSupport::Concern
27
+ module ModelHelpers
28
+ extend ActiveSupport::Concern
28
29
 
29
- included do
30
- attachment_names.each do |name|
31
- attr_accessor "#{name}_attachment".to_sym,
32
- "#{name}_attachment_remove".to_sym
33
- next unless active_asset_column_names.any?
34
- active_asset_column_names.each do |col|
35
- attr_accessor "#{name}_attachment_#{col}".to_sym
30
+ included do
31
+ attachment_names.each do |name|
32
+ attr_accessor "#{name}_attachment".to_sym,
33
+ "#{name}_attachment_remove".to_sym
34
+ next unless active_asset_column_names.any?
35
+ active_asset_column_names.each do |col|
36
+ attr_accessor "#{name}_attachment_#{col}".to_sym
37
+ end
36
38
  end
39
+
40
+ validate :process_attachments, unless: :new_record?
41
+ validate :process_multiple_attachments, unless: :new_record?
42
+
43
+ after_save :process_attachments
44
+ after_save :process_multiple_attachments
37
45
  end
38
46
 
39
- validate :process_attachments, unless: :new_record?
40
- validate :process_multiple_attachments, unless: :new_record?
47
+ # ClassMethods
48
+ module ClassMethods
49
+ def active_asset_column_names
50
+ asset_column_names - excluded_asset_column_names
51
+ end
52
+
53
+ def asset_column_names
54
+ Asset.column_names
55
+ end
41
56
 
42
- after_save :process_attachments
43
- after_save :process_multiple_attachments
44
- end
57
+ def attachment_names
58
+ [:asset]
59
+ end
45
60
 
46
- # ClassMethods
47
- module ClassMethods
48
- def active_asset_column_names
49
- asset_column_names - excluded_asset_column_names
50
- end
61
+ def excluded_asset_column_names
62
+ %w(id attachment assetable_id assetable_type created_at updated_at)
63
+ end
51
64
 
52
- def asset_column_names
53
- Asset.column_names
65
+ def multiple_attachment_names
66
+ [:downloadables]
67
+ end
54
68
  end
55
69
 
56
- def attachment_names
57
- [:asset]
70
+ private
71
+
72
+ def active_asset_column_names
73
+ self.class.active_asset_column_names
58
74
  end
59
75
 
60
- def excluded_asset_column_names
61
- %w(id attachment assetable_id assetable_type created_at updated_at)
76
+ def add_asset_data(a)
77
+ return unless active_asset_column_names.any?
78
+ active_asset_column_names.each do |col|
79
+ add_asset_field a, col
80
+ end
62
81
  end
63
82
 
64
- def multiple_attachment_names
65
- [:downloadables]
83
+ def add_asset_field(a, col)
84
+ n = "#{name}_attachment_#{col}".to_sym
85
+ return unless send(n).present?
86
+ a.send("#{col}=", send(n))
66
87
  end
67
- end
68
88
 
69
- private
89
+ def attachment_name(name)
90
+ "#{name}_attachment".to_sym
91
+ end
70
92
 
71
- def active_asset_column_names
72
- self.class.active_asset_column_names
73
- end
93
+ def attachment_name_remove(name)
94
+ "#{name}_attachment_remove".to_sym
95
+ end
74
96
 
75
- def add_asset_data(a)
76
- return unless active_asset_column_names.any?
77
- active_asset_column_names.each do |col|
78
- add_asset_field a, col
97
+ def attachment_names
98
+ self.class.attachment_names
79
99
  end
80
- end
81
100
 
82
- def add_asset_field(a, col)
83
- n = "#{name}_attachment_#{col}".to_sym
84
- return unless send(n).present?
85
- a.send("#{col}=", send(n))
86
- end
101
+ def multiple_attachment_names
102
+ self.class.multiple_attachment_names
103
+ end
87
104
 
88
- def attachment_name(name)
89
- "#{name}_attachment".to_sym
90
- end
105
+ def process_attachment(name)
106
+ n = attachment_name(name)
107
+ a = Asset.new(attachment: send(n), assetable: self)
108
+ add_asset_data(a)
109
+ if a.save
110
+ send("#{name}=", a)
111
+ send("#{n}=", nil)
112
+ else
113
+ errors.add(name, a.errors.full_messages.join(','))
114
+ false
115
+ end
116
+ end
91
117
 
92
- def attachment_name_remove(name)
93
- "#{name}_attachment_remove".to_sym
94
- end
118
+ def process_attachment?(name)
119
+ n = attachment_name(name)
120
+ respond_to?(n) && send(n).present?
121
+ end
95
122
 
96
- def attachment_names
97
- self.class.attachment_names
98
- end
123
+ def process_attachment_name(name)
124
+ process_attachment(name) if process_attachment?(name)
125
+ remove_attachment(name) if remove_attachment?(name)
126
+ end
99
127
 
100
- def multiple_attachment_names
101
- self.class.multiple_attachment_names
102
- end
128
+ def process_attachments
129
+ return if attachment_names.empty?
130
+ attachment_names.map { |n| process_attachment_name n }
131
+ end
103
132
 
104
- def process_attachment(name)
105
- n = attachment_name(name)
106
- a = Asset.new(attachment: send(n), assetable: self)
107
- add_asset_data(a)
108
- if a.save
109
- send("#{name}=", a)
133
+ def remove_attachment(name)
134
+ return unless send(name).destroy
135
+ n = attachment_name_remove(name)
136
+ send("#{name}=", nil)
110
137
  send("#{n}=", nil)
111
- else
112
- errors.add(name, a.errors.full_messages.join(','))
113
- false
114
138
  end
115
- end
116
-
117
- def process_attachment?(name)
118
- n = attachment_name(name)
119
- respond_to?(n) && send(n).present?
120
- end
121
-
122
- def process_attachment_name(name)
123
- process_attachment(name) if process_attachment?(name)
124
- remove_attachment(name) if remove_attachment?(name)
125
- end
126
139
 
127
- def process_attachments
128
- return if attachment_names.empty?
129
- attachment_names.map { |n| process_attachment_name n }
130
- end
131
-
132
- def remove_attachment(name)
133
- return unless send(name).destroy
134
- n = attachment_name_remove(name)
135
- send("#{name}=", nil)
136
- send("#{n}=", nil)
137
- end
138
-
139
- def remove_attachment?(name)
140
- n = attachment_name_remove(name)
141
- respond_to?(n) &&
142
- send(n).present? &&
143
- send(n).to_i > 0 &&
144
- send(name).present?
145
- end
140
+ def remove_attachment?(name)
141
+ n = attachment_name_remove(name)
142
+ respond_to?(n) &&
143
+ send(n).present? &&
144
+ send(n).to_i > 0 &&
145
+ send(name).present?
146
+ end
146
147
 
147
- # rubocop:disable all
148
- def process_multiple_attachments
149
- return if multiple_attachment_names.empty?
150
- multiple_attachment_names.each do |thing|
151
- at = "#{thing.to_s.singularize}_attachment".to_sym
152
- alt = "#{thing.to_s.singularize}_attachment_alt".to_sym
153
- att = "#{thing.to_s.singularize}_attachment_title".to_sym
154
- atr = "#{thing.to_s.singularize}_attachment_remove".to_sym
155
- atu = "#{thing.to_s.singularize}_attachment_url".to_sym
156
- if respond_to?(thing) && respond_to?(at) && send(at).present?
157
- a = Asset.new(attachment: send(at), title: send(att), alt: send(alt), assetable_type: self.class.name, assetable_id: id, url: send(atu))
158
- if a.alt.present? && a.title.blank?
159
- a.title = a.alt
160
- elsif a.alt.blank? && a.title.present?
161
- a.alt = a.title
162
- end
163
- if a.save
164
- send("#{at}=", nil)
165
- send("#{alt}=", nil)
166
- send("#{att}=", nil)
167
- send("#{atr}=", nil)
168
- send("#{atu}=", nil)
169
- send("#{thing.to_s.pluralize.to_sym}=", send("#{thing.to_s.pluralize.to_sym}") + [a])
170
- else
171
- errors.add(thing, a.errors.full_messages.join(','))
172
- return false
148
+ # rubocop:disable all
149
+ def process_multiple_attachments
150
+ return if multiple_attachment_names.empty?
151
+ multiple_attachment_names.each do |thing|
152
+ at = "#{thing.to_s.singularize}_attachment".to_sym
153
+ alt = "#{thing.to_s.singularize}_attachment_alt".to_sym
154
+ att = "#{thing.to_s.singularize}_attachment_title".to_sym
155
+ atr = "#{thing.to_s.singularize}_attachment_remove".to_sym
156
+ atu = "#{thing.to_s.singularize}_attachment_url".to_sym
157
+ if respond_to?(thing) && respond_to?(at) && send(at).present?
158
+ a = Asset.new(attachment: send(at), title: send(att), alt: send(alt), assetable_type: self.class.name, assetable_id: id, url: send(atu))
159
+ if a.alt.present? && a.title.blank?
160
+ a.title = a.alt
161
+ elsif a.alt.blank? && a.title.present?
162
+ a.alt = a.title
163
+ end
164
+ if a.save
165
+ send("#{at}=", nil)
166
+ send("#{alt}=", nil)
167
+ send("#{att}=", nil)
168
+ send("#{atr}=", nil)
169
+ send("#{atu}=", nil)
170
+ send("#{thing.to_s.pluralize.to_sym}=", send("#{thing.to_s.pluralize.to_sym}") + [a])
171
+ else
172
+ errors.add(thing, a.errors.full_messages.join(','))
173
+ return false
174
+ end
175
+ elsif respond_to?(atr) && send(atr).present? && send(atr).to_i > 0 && send(thing).present?
176
+ send(thing).destroy_all
173
177
  end
174
- elsif respond_to?(atr) && send(atr).present? && send(atr).to_i > 0 && send(thing).present?
175
- send(thing).destroy_all
176
178
  end
177
179
  end
180
+ # rubocop:enable all
178
181
  end
179
- # rubocop:enable all
180
182
  end
@@ -1,3 +1,3 @@
1
1
  module MorseAssetable
2
- VERSION = "0.2.1"
2
+ VERSION = "1.0.0"
3
3
  end
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: morse_assetable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Terry S
@@ -72,6 +72,7 @@ files:
72
72
  - bin/setup
73
73
  - lib/morse_assetable.rb
74
74
  - lib/morse_assetable/version.rb
75
+ - morse_assetable-0.2.2.gem
75
76
  - morse_assetable.gemspec
76
77
  homepage: https://github.com/morsedigital/morse_assetable.git
77
78
  licenses: