cadmus_files 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +10 -0
- data/app/helpers/cadmus_files/admin_helper.rb +16 -0
- data/app/views/cadmus/files/index.html.erb +48 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/cadmus_files.gemspec +31 -0
- data/lib/cadmus_files.rb +24 -0
- data/lib/cadmus_files/admin_controller.rb +38 -0
- data/lib/cadmus_files/engine.rb +7 -0
- data/lib/cadmus_files/file.rb +44 -0
- data/lib/cadmus_files/file_url_tag.rb +31 -0
- data/lib/cadmus_files/version.rb +3 -0
- metadata +157 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bb30739f67050580cdc34306a443a103009b8b7f
|
4
|
+
data.tar.gz: ae0f9f9bb0855f9d93f6fddde7cd22fff02df114
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a8ce7909deb16aae83534260e270e95d8b0fc89fa407a35a15ac7bceb618f2645b49027b24f60b2e6f3a72c6b087e20cfac46faab2ee219592a65d349d698a89
|
7
|
+
data.tar.gz: 5943e016986d994e8fd7c32c80293990a47f3676e3fd4dbb1cf0db81139231b047f9246f0a88242e7965e980792b0c466f8c87392a8006c80a2b36533e7eae07
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# CadmusFiles
|
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/cadmus_files`. 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 'cadmus_files'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install cadmus_files
|
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 test` 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]/cadmus_files.
|
36
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module CadmusFiles::AdminHelper
|
2
|
+
def icon_for_content_type(content_type)
|
3
|
+
case content_type
|
4
|
+
when %r(\Aaudio/) then 'file-audio-o'
|
5
|
+
when %r(\Avideo/) then 'file-video-o'
|
6
|
+
when 'application/pdf' then 'file-pdf-o'
|
7
|
+
when 'application/zip', 'application/x-bzip', 'application/x-bzip2', 'application/x-tar', 'application/x-7z-compressed'
|
8
|
+
'file-zip-o'
|
9
|
+
when 'application/msword' then 'file-word-o'
|
10
|
+
when 'application/vnd.ms-excel' then 'file-excel-o'
|
11
|
+
when 'application/vnd.ms-powerpoint' then 'file-powerpoint-o'
|
12
|
+
when 'text/plain', 'application/rtf' then 'file-text-o'
|
13
|
+
else 'file-o'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
<h1 class="mb-4">Files</h1>
|
2
|
+
|
3
|
+
<% @cms_files.each_slice(4) do |slice| -%>
|
4
|
+
<div class="card-deck mb-4">
|
5
|
+
<% slice.each do |cms_file| -%>
|
6
|
+
<% file = cms_file.public_send(cms_file.class.cadmus_file_field) -%>
|
7
|
+
|
8
|
+
<div class="card">
|
9
|
+
<div class="card-header">
|
10
|
+
<div class="float-right">
|
11
|
+
<%= link_to url_for(action: 'destroy', id: cms_file), class: 'btn btn-secondary px-2 py-1', method: 'DELETE', "data-confirm" => "Are you sure you want to delete #{cms_file.filename}?" do %>
|
12
|
+
<i class="fa fa-trash" aria-hidden="true"></i>
|
13
|
+
<% end -%>
|
14
|
+
</div>
|
15
|
+
<small style="font-weight: bold; word-wrap: break-word;">
|
16
|
+
<%= cms_file.filename %>
|
17
|
+
</small>
|
18
|
+
</div>
|
19
|
+
<div class="card-block text-center">
|
20
|
+
<%= link_to file.url do %>
|
21
|
+
<% if cms_file.is_image? -%>
|
22
|
+
<%= image_tag file.url, class: 'img-responsive', style: 'max-height: 100px; max-width: 100px;' %>
|
23
|
+
<% else -%>
|
24
|
+
<i class="fa fa-<%= icon_for_content_type(file.content_type) %>" style="font-size: 100px" aria-hidden="true"></i>
|
25
|
+
<% end -%>
|
26
|
+
<% end %>
|
27
|
+
</div>
|
28
|
+
<div class="card-footer text-right">
|
29
|
+
<small>
|
30
|
+
<%= number_to_human_size file.size %>
|
31
|
+
</small>
|
32
|
+
</div>
|
33
|
+
</div>
|
34
|
+
<% end -%>
|
35
|
+
</div>
|
36
|
+
<% end -%>
|
37
|
+
|
38
|
+
<div class="card">
|
39
|
+
<div class="card-header">Upload a file</div>
|
40
|
+
<div class="card-block">
|
41
|
+
<%= form_for :cms_file, url: url_for(action: 'create'), method: 'POST', multipart: true do |f| %>
|
42
|
+
<div class="form-group">
|
43
|
+
<%= f.file_field :file, class: 'form-control-file' %>
|
44
|
+
</div>
|
45
|
+
<%= f.submit "Upload", class: 'btn btn-primary' %>
|
46
|
+
<% end %>
|
47
|
+
</div>
|
48
|
+
</div>
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "cadmus_files"
|
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,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cadmus_files/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cadmus_files"
|
8
|
+
spec.version = CadmusFiles::VERSION
|
9
|
+
spec.authors = ["Nat Budin"]
|
10
|
+
spec.email = ["natbudin@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{File uploads for Cadmus sites}
|
13
|
+
spec.description = %q{Adds file uploading and linking to the Cadmus micro-CMS using Carrierwave}
|
14
|
+
spec.homepage = "https://github.com/nbudin/cadmus_files"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.required_ruby_version = ">= 2.0"
|
22
|
+
|
23
|
+
spec.add_dependency "rails", ">= 4.0.0"
|
24
|
+
spec.add_dependency "cadmus"
|
25
|
+
spec.add_dependency "liquid"
|
26
|
+
spec.add_dependency "carrierwave"
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "minitest"
|
31
|
+
end
|
data/lib/cadmus_files.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "cadmus_files/version"
|
2
|
+
require "cadmus_files/engine"
|
3
|
+
require "cadmus_files/file"
|
4
|
+
require "cadmus_files/file_url_tag"
|
5
|
+
require "cadmus_files/admin_controller"
|
6
|
+
|
7
|
+
module CadmusFiles
|
8
|
+
class << self
|
9
|
+
def file_model
|
10
|
+
@_file_model ||= @_file_model_name.safe_constantize
|
11
|
+
end
|
12
|
+
|
13
|
+
def file_model=(file_model)
|
14
|
+
@_file_model_name = case file_model
|
15
|
+
when Class then file_model.name
|
16
|
+
else file_model
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def clear_file_model_cache!
|
21
|
+
@_file_model = nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module CadmusFiles
|
2
|
+
module AdminController
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
helper 'cadmus_files/admin'
|
7
|
+
end
|
8
|
+
|
9
|
+
def index
|
10
|
+
@cms_files ||= cms_file_scope
|
11
|
+
render template: 'cadmus/files/index'
|
12
|
+
end
|
13
|
+
|
14
|
+
def create
|
15
|
+
@cms_file ||= cms_file_scope.new(cms_file_params)
|
16
|
+
@cms_file.save!
|
17
|
+
|
18
|
+
redirect_to action: 'index'
|
19
|
+
end
|
20
|
+
|
21
|
+
def destroy
|
22
|
+
@cms_file ||= cms_file_scope.find(params[:id])
|
23
|
+
@cms_file.destroy
|
24
|
+
|
25
|
+
redirect_to action: 'index'
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def cms_file_scope
|
31
|
+
CadmusFiles.file_model.all
|
32
|
+
end
|
33
|
+
|
34
|
+
def cms_file_params
|
35
|
+
params.require(:cms_file).permit(:file)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module CadmusFiles
|
2
|
+
module File
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def cadmus_file(file_field = 'file')
|
7
|
+
class << self
|
8
|
+
attr_accessor :cadmus_file_field
|
9
|
+
end
|
10
|
+
self.cadmus_file_field = file_field
|
11
|
+
|
12
|
+
belongs_to :parent, polymorphic: true
|
13
|
+
|
14
|
+
validates_integrity_of file_field
|
15
|
+
validates_processing_of file_field
|
16
|
+
validate :validate_file_name_is_unique
|
17
|
+
|
18
|
+
define_method :to_param do
|
19
|
+
"#{id}-#{filename.to_param}"
|
20
|
+
end
|
21
|
+
|
22
|
+
define_method :filename do
|
23
|
+
::File.basename(send(file_field).path)
|
24
|
+
end
|
25
|
+
|
26
|
+
define_method :is_image? do
|
27
|
+
send(file_field).content_type =~ /\Aimage\//
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
define_method :validate_file_name_is_unique do
|
33
|
+
the_filename = send(file_field).filename
|
34
|
+
|
35
|
+
if self.class.base_class.where(file_field => the_filename).any?
|
36
|
+
errors.add file_field, "'#{the_filename}' already exists"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
ActiveRecord::Base.send :include, CadmusFiles::File
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'liquid'
|
2
|
+
|
3
|
+
module CadmusFiles
|
4
|
+
class FileUrlTag < Liquid::Tag
|
5
|
+
attr_reader :filename
|
6
|
+
|
7
|
+
def initialize(tag_name, args, tokens)
|
8
|
+
super
|
9
|
+
@filename = args.strip.gsub(/\A([\"\'])(.*)\1\z/, '\2')
|
10
|
+
end
|
11
|
+
|
12
|
+
def render(context)
|
13
|
+
file_model = CadmusFiles.file_model
|
14
|
+
|
15
|
+
unless file_model
|
16
|
+
return "Error: CadmusFiles.file_model not set. Please set it to your file model in an initializer."
|
17
|
+
end
|
18
|
+
|
19
|
+
parent = context.registers['parent']
|
20
|
+
cms_file = file_model.find_by(file_model.cadmus_file_field => filename, parent: parent)
|
21
|
+
unless cms_file
|
22
|
+
return "Error: file #{filename} not found"
|
23
|
+
end
|
24
|
+
|
25
|
+
cms_file.file.url
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
Liquid::Template.register_tag('file_url', FileUrlTag)
|
30
|
+
end
|
31
|
+
|
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cadmus_files
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nat Budin
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: cadmus
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: liquid
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: carrierwave
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.10'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.10'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: minitest
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Adds file uploading and linking to the Cadmus micro-CMS using Carrierwave
|
112
|
+
email:
|
113
|
+
- natbudin@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".travis.yml"
|
120
|
+
- Gemfile
|
121
|
+
- README.md
|
122
|
+
- Rakefile
|
123
|
+
- app/helpers/cadmus_files/admin_helper.rb
|
124
|
+
- app/views/cadmus/files/index.html.erb
|
125
|
+
- bin/console
|
126
|
+
- bin/setup
|
127
|
+
- cadmus_files.gemspec
|
128
|
+
- lib/cadmus_files.rb
|
129
|
+
- lib/cadmus_files/admin_controller.rb
|
130
|
+
- lib/cadmus_files/engine.rb
|
131
|
+
- lib/cadmus_files/file.rb
|
132
|
+
- lib/cadmus_files/file_url_tag.rb
|
133
|
+
- lib/cadmus_files/version.rb
|
134
|
+
homepage: https://github.com/nbudin/cadmus_files
|
135
|
+
licenses: []
|
136
|
+
metadata: {}
|
137
|
+
post_install_message:
|
138
|
+
rdoc_options: []
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '2.0'
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
requirements: []
|
152
|
+
rubyforge_project:
|
153
|
+
rubygems_version: 2.6.4
|
154
|
+
signing_key:
|
155
|
+
specification_version: 4
|
156
|
+
summary: File uploads for Cadmus sites
|
157
|
+
test_files: []
|