ds_media_library 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +2 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +10 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +77 -0
  10. data/Rakefile +9 -0
  11. data/app/assets/javascripts/ds_media_library.coffee +45 -0
  12. data/app/assets/javascripts/toggle_all_checkboxes.coffee +6 -0
  13. data/app/controllers/ds_media_library/folders_controller.rb +37 -0
  14. data/app/controllers/ds_media_library/resources_controller.rb +71 -0
  15. data/app/models/ds_media_library/folder.rb +43 -0
  16. data/app/views/ds_media_library/_folder.html.slim +11 -0
  17. data/app/views/ds_media_library/_form_helper.html.slim +30 -0
  18. data/app/views/ds_media_library/_image_preview.html.slim +17 -0
  19. data/app/views/ds_media_library/_resource.html.slim +27 -0
  20. data/app/views/ds_media_library/resources/_folder.html.slim +14 -0
  21. data/app/views/ds_media_library/resources/_resource.html.slim +12 -0
  22. data/app/views/ds_media_library/resources/folder_form.html.slim +19 -0
  23. data/app/views/ds_media_library/resources/form.html.slim +31 -0
  24. data/app/views/ds_media_library/resources/index.html.slim +28 -0
  25. data/app/views/shared/_image_preview.html.slim +17 -0
  26. data/app/views/shared/_submit.html.slim +6 -0
  27. data/bin/console +14 -0
  28. data/bin/server +5 -0
  29. data/bin/setup +5 -0
  30. data/config/routes.rb +8 -0
  31. data/ds_media_library.gemspec +44 -0
  32. data/dummy/app.rb +53 -0
  33. data/dummy/database.rb +55 -0
  34. data/dummy/views.rb +114 -0
  35. data/lib/ds_media_library/engine.rb +32 -0
  36. data/lib/ds_media_library/form_helper.rb +86 -0
  37. data/lib/ds_media_library/version.rb +3 -0
  38. data/lib/ds_media_library.rb +10 -0
  39. data/tmp/.keep +0 -0
  40. metadata +348 -0
@@ -0,0 +1,44 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ds_media_library/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ds_media_library"
8
+ spec.version = DSMediaLibrary::VERSION
9
+ spec.authors = ["Micah Geisel"]
10
+ spec.email = ["micah@botandrose.com"]
11
+
12
+ spec.summary = 'Downstream Media Library Rails Engine'
13
+ spec.description = 'Downstream Media Library Rails Engine'
14
+ spec.homepage = "https://github.com/botandrose/ds_media_library"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.14"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "cucumber-rails"
28
+ spec.add_development_dependency "chop"
29
+ spec.add_development_dependency "sqlite3"
30
+ spec.add_development_dependency "poltergeist"
31
+ spec.add_development_dependency "phantomjs"
32
+ spec.add_development_dependency "capybara-screenshot"
33
+ spec.add_development_dependency "database_cleaner"
34
+ spec.add_development_dependency "byebug"
35
+
36
+ spec.add_dependency "rails"
37
+ spec.add_dependency "slim"
38
+ spec.add_dependency "ds_node"
39
+ spec.add_dependency "awesome_nested_set"
40
+ spec.add_dependency "sass-rails"
41
+ spec.add_dependency "coffee-rails"
42
+ spec.add_dependency "jquery-rails"
43
+ spec.add_dependency "jquery-ui-rails"
44
+ end
data/dummy/app.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'bundler/setup'
2
+ require "rails/all"
3
+ require "ds_media_library"
4
+
5
+ module Dummy; end
6
+
7
+ class Dummy::App < Rails::Application
8
+ config.secret_key_base = "test"
9
+ config.eager_load = false
10
+ config.logger = Logger.new("/dev/stdout")
11
+ config.logger = Logger.new("/dev/null")
12
+ end
13
+
14
+ ENV["DATABASE_URL"] = "sqlite3:tmp/test.sqlite3"
15
+
16
+ Dummy::App.initialize!
17
+
18
+ class ApplicationController < ActionController::Base
19
+ def self.expose *methods
20
+ methods.each do |method|
21
+ attr_reader method
22
+ helper_method method
23
+ end
24
+ end
25
+
26
+ layout "test"
27
+
28
+ def show
29
+ @widget = Widget.first_or_create!
30
+ end
31
+
32
+ def update
33
+ Widget.first_or_create!.update! params.require(:widget).permit!
34
+ redirect_to "/", notice: "Widget updated"
35
+ end
36
+ end
37
+
38
+ Dummy::App.routes.draw do
39
+ mount DSMediaLibrary::Engine => "/media_library"
40
+ root to: "application#show"
41
+ patch "/" => "application#update"
42
+ end
43
+
44
+ # app's soft delete leaves files in place, so clean them up
45
+ at_exit do
46
+ begin
47
+ Rails.public_path.rmtree
48
+ rescue Errno::ENOENT
49
+ end
50
+ end
51
+
52
+ require_relative "./database"
53
+ require_relative "./views"
data/dummy/database.rb ADDED
@@ -0,0 +1,55 @@
1
+ # sqlite3 hates our mysql indexes
2
+ ActiveRecord::Migration.class_eval do
3
+ def add_index *; end
4
+
5
+ def create_table name, options
6
+ options.delete(:options)
7
+ super
8
+ end
9
+ end
10
+
11
+ ActiveRecord::Migration.suppress_messages do
12
+ ActiveRecord::Schema.define(version: 20170822210346) do
13
+ create_table "widgets", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
14
+ t.integer "cat_picture_id"
15
+ t.string "dog_picture_ids"
16
+ end
17
+
18
+ create_table "folders", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
19
+ t.string "name"
20
+ t.integer "parent_id"
21
+ t.integer "lft", null: false
22
+ t.integer "rgt", null: false
23
+ t.datetime "created_at", null: false
24
+ t.datetime "updated_at", null: false
25
+ t.integer "depth", default: 0, null: false
26
+ t.index ["lft"], name: "index_folders_on_lft"
27
+ t.index ["parent_id"], name: "index_folders_on_parent_id"
28
+ t.index ["rgt"], name: "index_folders_on_rgt"
29
+ end
30
+
31
+ create_table "resources", primary_key: "resourcesid", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
32
+ t.string "resourcestype"
33
+ t.decimal "resourcesduration", precision: 10, default: "0"
34
+ t.integer "resourceswidth", default: 0
35
+ t.integer "resourcesheight", default: 0
36
+ t.string "resourcesfilename"
37
+ t.string "resourcesoriginalfilename"
38
+ t.string "resourcespath", default: "resources/"
39
+ t.integer "resourcesthumbid"
40
+ t.string "resourceshash"
41
+ t.integer "resourcesparentid", default: 0
42
+ t.integer "resourcesparentindex", default: 0
43
+ t.datetime "created_at", null: false
44
+ t.datetime "updated_at", null: false
45
+ t.integer "folder_id"
46
+ t.boolean "hidden", default: false
47
+ end
48
+ end
49
+ end
50
+
51
+ class Widget < ActiveRecord::Base
52
+ ds_resource :cat_picture
53
+ belongs_to_many_ds_resources :dog_pictures
54
+ end
55
+
data/dummy/views.rb ADDED
@@ -0,0 +1,114 @@
1
+ class InMemoryResolver < ActionView::Resolver
2
+ cattr_accessor(:store) { Hash.new }
3
+
4
+ def find_templates(name, prefix, partial, details, outside_app_allowed = false)
5
+ key = [prefix, name].join("/")
6
+ if contents = store[key]
7
+ [ActionView::Template.new(
8
+ contents,
9
+ "app/views/#{key}",
10
+ ActionView::Template.handler_for_extension(:slim),
11
+ virtual_path: key,
12
+ format: :html,
13
+ updated_at: Time.zone.now,
14
+ )]
15
+ else
16
+ []
17
+ end
18
+ end
19
+ end
20
+
21
+ ApplicationController.prepend_view_path InMemoryResolver.new
22
+
23
+ InMemoryResolver.store["layouts/test"] = <<-SLIM
24
+ sass:
25
+ .hidden
26
+ display: none
27
+
28
+ .media-nest
29
+ display: none
30
+
31
+ .expand-input:checked ~ .media-nest
32
+ display: block
33
+
34
+ .media-button, .flash-notice, .flash-alert
35
+ text-transform: uppercase
36
+
37
+ .modal-input
38
+ opacity: 0.001
39
+ &:checked ~ .modal-wrapper
40
+ left: 0
41
+ opacity: 1
42
+ .modal-content
43
+ display: inline-block
44
+ .modal-bg
45
+ background: white
46
+ opacity: 1
47
+
48
+ .modal-wrapper
49
+ position: fixed
50
+ display: -webkit-flex
51
+ display: flex
52
+ top: 0
53
+ left: -100vw
54
+ width: 100vw
55
+ height: 100vh
56
+ z-index: 9999
57
+ text-align: left
58
+
59
+ .modal-bg
60
+ background: rgba(#808080, 0.85)
61
+ opacity: 0
62
+ position: absolute
63
+ z-index: 2
64
+ width: 100vw
65
+ height: 100vh
66
+ top: 0
67
+ left: 0
68
+
69
+ .modal-content
70
+ display: none
71
+ margin: auto 0
72
+ width: 94vw
73
+ height: 100vh
74
+ padding: 40px
75
+ z-index: 3
76
+
77
+ .modal-close
78
+ position: absolute
79
+ top: -10px
80
+ right: 40px
81
+ margin: 0
82
+ width: 40px
83
+ height: 40px
84
+ line-height: 40px
85
+ text-align: center
86
+
87
+ h1 TEST
88
+
89
+ nav
90
+ ul
91
+ li = link_to "Widget", "/"
92
+ li = link_to "Manage Media Library", "/media_library"
93
+
94
+ .flash-notice = flash.notice
95
+ .flash-alert = flash.alert
96
+
97
+ = yield
98
+
99
+ = javascript_include_tag "ds_media_library"
100
+ SLIM
101
+
102
+ InMemoryResolver.store["application/show"] = <<-SLIM
103
+ = form_for @widget, url: "/" do |form|
104
+ .field
105
+ = form.label :cat_picture
106
+ = form.media_library :cat_picture
107
+
108
+ .field
109
+ = form.label :dog_pictures
110
+ = form.media_library :dog_pictures, multiple: true
111
+
112
+ = form.submit "Save"
113
+ SLIM
114
+
@@ -0,0 +1,32 @@
1
+ require "ds_node"
2
+ require "slim"
3
+ require "jquery-rails"
4
+ require "jquery-ui-rails"
5
+ require "coffee-rails"
6
+
7
+ module DSMediaLibrary
8
+ class Engine < Rails::Engine
9
+ initializer "ds_media_library.form_helper" do
10
+ ActionView::Helpers::FormBuilder.class_eval do
11
+ prepend FormHelper
12
+ end
13
+ end
14
+
15
+ initializer "ds_media_library.ds_node" do
16
+ DSNode::Resource.class_eval do
17
+ belongs_to :folder, class_name: "DSMediaLibrary::Folder", required: false
18
+
19
+ def css_class
20
+ "media-link"
21
+ end
22
+ end
23
+ end
24
+
25
+ initializer "ds_media_library.assets" do
26
+ config.assets.precompile += %w( ds_media_library.js )
27
+ end
28
+
29
+ isolate_namespace DSMediaLibrary
30
+ end
31
+ end
32
+
@@ -0,0 +1,86 @@
1
+ require "active_support/number_helper"
2
+
3
+ module DSMediaLibrary
4
+ module FormHelper
5
+ def media_library field, label: field.to_s.humanize, multiple: false, optional: false, dimensions: nil, helptext: nil, preview: true, required: false, &block
6
+ helper = MediaLibrary.new(object.send(field), field, label, multiple, optional, dimensions, helptext, preview, required, block)
7
+ @template.render "ds_media_library/form_helper", form: self, helper: helper
8
+ end
9
+
10
+ class MediaLibrary < Struct.new(:object, :field, :label, :multiple, :optional, :dimensions, :helptext, :preview, :required, :block)
11
+ def ids
12
+ Array(object).map(&:id)
13
+ end
14
+
15
+ def single_field
16
+ field.to_s.singularize.to_sym
17
+ end
18
+
19
+ def file_field_name
20
+ if multiple
21
+ :"new_#{single_field}_files"
22
+ else
23
+ :"#{field}_file"
24
+ end
25
+ end
26
+
27
+ def remove_field_name
28
+ if multiple
29
+ :"remove_#{single_field}_ids"
30
+ else
31
+ :"remove_#{field}"
32
+ end
33
+ end
34
+
35
+ def field_id_name
36
+ if multiple
37
+ :"#{single_field}_ids"
38
+ else
39
+ :"#{field}_id"
40
+ end
41
+ end
42
+
43
+ def html_id
44
+ "media-modal-#{field}"
45
+ end
46
+
47
+ def label_css_class
48
+ "optional" if optional
49
+ end
50
+
51
+ def helptext
52
+ super || dimensions.present? && "#{label.pluralize} must be #{number_with_delimiter(width)}px wide by #{number_with_delimiter(height)}px tall."
53
+ end
54
+
55
+ def width
56
+ dimensions.split("x").first if dimensions
57
+ end
58
+
59
+ def height
60
+ dimensions.split("x").last if dimensions
61
+ end
62
+
63
+ def root
64
+ @root ||= Folder.root
65
+ end
66
+
67
+ def root_folders
68
+ root.children
69
+ end
70
+
71
+ def root_resources
72
+ root.resources
73
+ end
74
+
75
+ def original_file_name
76
+ object.original_file_name if object.present?
77
+ end
78
+
79
+ private
80
+
81
+ def number_with_delimiter number
82
+ ActiveSupport::NumberHelper.number_to_delimited(number)
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module DSMediaLibrary
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "ds_media_library/version"
2
+ require "ds_media_library/form_helper"
3
+ require "ds_media_library/engine"
4
+
5
+ module DSMediaLibrary
6
+ end
7
+
8
+ # HACK work around autoloading sometimes demanding this specific inflection
9
+ DsMediaLibrary = DSMediaLibrary
10
+
data/tmp/.keep ADDED
File without changes