morse_assetable 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c9939f9e3588f2fdc9e0407c334f65c35c918af7
4
+ data.tar.gz: e664776540d35cd88a7166fea8e18bae2cf744bf
5
+ SHA512:
6
+ metadata.gz: 0f5a3f2c973229c8653cb26cee5d4b07433d189e5ea39df7dc16c0ff6baf2009865e662494d3351b785e2317e3123fba8eaf3e503b0d3c04e215f3cd1b8c6f59
7
+ data.tar.gz: 226a71800f258fa4eb8736c6114f474e93287ebc7328c561f55e783812983c8059e9de7c6616c224a3c63d88442171382780886bb6b655b5e4ed7054089e1666
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in morse_assetable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Terry S
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,41 @@
1
+ # MorseAssetable
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/morse_assetable`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'morse_assetable'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install morse_assetable
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/morse_assetable.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "morse_assetable"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module MorseAssetable
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,155 @@
1
+ require "morse_assetable/version"
2
+
3
+ # MorseAssetable
4
+ module MorseAssetable # rubocop:disable Metrics/ModuleLength
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ attachment_names.each do |name|
9
+ attr_accessor "#{name}_attachment".to_sym,
10
+ "#{name}_attachment_remove".to_sym
11
+ next unless active_asset_column_names.any?
12
+ active_asset_column_names.each do |col|
13
+ attr_accessor "#{name}_attachment_#{col}".to_sym
14
+ end
15
+ end
16
+
17
+ validate :process_attachments
18
+ validate :process_multiple_attachments
19
+ end
20
+
21
+ # ClassMethods
22
+ module ClassMethods
23
+ def active_asset_column_names
24
+ asset_column_names - excluded_asset_column_names
25
+ end
26
+
27
+ def asset_column_names
28
+ Asset.column_names
29
+ end
30
+
31
+ def attachment_names
32
+ [:asset]
33
+ end
34
+
35
+ def excluded_asset_column_names
36
+ %w(id attachment assetable_id assetable_type created_at updated_at)
37
+ end
38
+
39
+ def multiple_attachment_names
40
+ [:downloadables]
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def active_asset_column_names
47
+ self.class.active_asset_column_names
48
+ end
49
+
50
+ def add_asset_data(a)
51
+ return unless active_asset_column_names.any?
52
+ active_asset_column_names.each do |col|
53
+ add_asset_field a, col
54
+ end
55
+ end
56
+
57
+ def add_asset_field(a, col)
58
+ n = "#{name}_attachment_#{col}".to_sym
59
+ return unless send(n).present?
60
+ a.send("#{col}=", send(n))
61
+ end
62
+
63
+ def attachment_name(name)
64
+ "#{name}_attachment".to_sym
65
+ end
66
+
67
+ def attachment_name_remove(name)
68
+ "#{name}_attachment_remove".to_sym
69
+ end
70
+
71
+ def attachment_names
72
+ self.class.attachment_names
73
+ end
74
+
75
+ def multiple_attachment_names
76
+ self.class.multiple_attachment_names
77
+ end
78
+
79
+ def process_attachment(name)
80
+ n = attachment_name(name)
81
+ a = Asset.new(attachment: send(n), assetable: self)
82
+ add_asset_data(a)
83
+ if a.save
84
+ send("#{name}=", a)
85
+ send("#{n}=", nil)
86
+ else
87
+ errors.add(thing, a.errors.full_messages.join(','))
88
+ false
89
+ end
90
+ end
91
+
92
+ def process_attachment?(name)
93
+ n = attachment_name(name)
94
+ respond_to?(n) && send(n).present?
95
+ end
96
+
97
+ def process_attachment_name(name)
98
+ process_attachment(name) if process_attachment?(name)
99
+ remove_attachment(name) if remove_attachment?(name)
100
+ end
101
+
102
+ def process_attachments
103
+ return if attachment_names.empty?
104
+ attachment_names.map { |n| process_attachment_name n }
105
+ end
106
+
107
+ def remove_attachment(name)
108
+ return unless send(name).destroy
109
+ n = attachment_name_remove(name)
110
+ send("#{name}=", nil)
111
+ send("#{n}=", nil)
112
+ end
113
+
114
+ def remove_attachment?(name)
115
+ n = attachment_name_remove(name)
116
+ respond_to?(n) &&
117
+ send(n).present? &&
118
+ send(n).to_i > 0 &&
119
+ send(name).present?
120
+ end
121
+
122
+ # rubocop:disable all
123
+ def process_multiple_attachments
124
+ return if multiple_attachment_names.empty?
125
+ multiple_attachment_names.each do |thing|
126
+ at = "#{thing.to_s.singularize}_attachment".to_sym
127
+ alt = "#{thing.to_s.singularize}_attachment_alt".to_sym
128
+ att = "#{thing.to_s.singularize}_attachment_title".to_sym
129
+ atr = "#{thing.to_s.singularize}_attachment_remove".to_sym
130
+ atu = "#{thing.to_s.singularize}_attachment_url".to_sym
131
+ if respond_to?(thing) && respond_to?(at) && send(at).present?
132
+ a = Asset.new(attachment: send(at), title: send(att), alt: send(alt), assetable_type: self.class.name, assetable_id: id, url: send(atu))
133
+ if a.alt.present? && a.title.blank?
134
+ a.title = a.alt
135
+ elsif a.alt.blank? && a.title.present?
136
+ a.alt = a.title
137
+ end
138
+ if a.save
139
+ send("#{at}=", nil)
140
+ send("#{alt}=", nil)
141
+ send("#{att}=", nil)
142
+ send("#{atr}=", nil)
143
+ send("#{atu}=", nil)
144
+ send("#{thing.to_s.pluralize.to_sym}=", send("#{thing.to_s.pluralize.to_sym}") + [a])
145
+ else
146
+ errors.add(thing, a.errors.full_messages.join(','))
147
+ return false
148
+ end
149
+ elsif respond_to?(atr) && send(atr).present? && send(atr).to_i > 0 && send(thing).present?
150
+ send(thing).destroy_all
151
+ end
152
+ end
153
+ end
154
+ # rubocop:enable all
155
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'morse_assetable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "morse_assetable"
8
+ spec.version = MorseAssetable::VERSION
9
+ spec.authors = ["Terry S"]
10
+ spec.email = ["itsterry@gmail.com"]
11
+
12
+ spec.summary = %q{ An easy way to handle submissions to carrierwave from a form }
13
+ spec.description = %q{ An easy way to handle submissions to carrierwave from a form }
14
+ spec.homepage = 'https://github.com/morsedigital/morse_assetable.git'
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: morse_assetable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Terry S
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: " An easy way to handle submissions to carrierwave from a form "
56
+ email:
57
+ - itsterry@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/morse_assetable.rb
72
+ - lib/morse_assetable/version.rb
73
+ - morse_assetable.gemspec
74
+ homepage: https://github.com/morsedigital/morse_assetable.git
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.6.4
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: An easy way to handle submissions to carrierwave from a form
98
+ test_files: []