damselfly 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OWNmMzhhYzVjYWNmYzAzOGU4MDRiZGQ0MWJjY2Q1ODU5MDY2YjZhNg==
5
+ data.tar.gz: !binary |-
6
+ MDJlMTk4MjIzYWFmYWE5YzBlOWY1NTMwN2UzZDIwYmE0ZTBkZWRiNA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ M2U5MGViYzk5ZmE2YzI0NmFlMzc2MDhkOTZkZmRmZjdhYTNkOGM5OTdmNmY2
10
+ OWUxMTgyMWY5NWQyYjViMDA1NTQxNjEyNGZmZTY4YmI5ZmQ3MTM1OTliZTU5
11
+ NDhiOTYxMGFiODI5YTkzNzkwMThkMWY4ODkzODZkMjliMmRkN2E=
12
+ data.tar.gz: !binary |-
13
+ NThlNzQ1MmYzZjFkOWNiZjFhMTM5ODJiY2Q1NGUzNGJiNTAzN2MxMDVmNjk3
14
+ N2E4NGE1NTJiZWYzYzNjYjI0N2RiNDlhY2UzOWUyOWNmZWYzZDgyMDI2MDBl
15
+ YzkyOTk1YWM0OWI3ODhhZDUwYzRiYjA4NjA1Nzc2ZGIwNmU1MTM=
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ = Damselfly
2
+
3
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Damselfly'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
@@ -0,0 +1,35 @@
1
+ module Damselfly
2
+ class Attachment < ActiveRecord::Base
3
+ belongs_to :file, class_name: "Damselfly::File"
4
+ belongs_to :attachable, polymorphic: true
5
+ delegate :data, :md5, :size, to: :file
6
+ before_validation :avoid_duplicates
7
+ after_destroy :delete_orphan_files
8
+ after_save :delete_nullified_attachments
9
+ validates_presence_of :association_name, :filename, :file
10
+
11
+ def data=(value)
12
+ self.file.file = value
13
+ self.filename = value.original_filename
14
+ end
15
+
16
+ def file
17
+ super || build_file
18
+ end
19
+
20
+ protected
21
+ def avoid_duplicates
22
+ if existing = File.where(md5: md5, size: size).first
23
+ self.file = existing
24
+ end
25
+ end
26
+
27
+ def delete_orphan_files
28
+ file.destroy if file.attachments.count.zero?
29
+ end
30
+
31
+ def delete_nullified_attachments
32
+ destroy unless attachable
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ module Damselfly
2
+ class File < ActiveRecord::Base
3
+ has_many :attachments
4
+
5
+ file_accessor :file do
6
+ after_assign do |f|
7
+ self.md5 = ::Digest::MD5.file(f.path).hexdigest
8
+ self.size = f.size
9
+ end
10
+ end
11
+
12
+ delegate :data, :size, to: :file
13
+ end
14
+ end
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,15 @@
1
+ class CreateDamselflyAttachments < ActiveRecord::Migration
2
+ def change
3
+ create_table :damselfly_attachments do |t|
4
+ t.string :association_name
5
+ t.string :filename
6
+ t.belongs_to :attachable, polymorphic: true
7
+ t.belongs_to :file
8
+
9
+ t.timestamps
10
+ end
11
+
12
+ add_index :damselfly_attachments, :file_id
13
+ add_index :damselfly_attachments, [:attachable_id, :attachable_type], name: "index_damselfly_on_attachable"
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ class CreateDamselflyFiles < ActiveRecord::Migration
2
+ def change
3
+ create_table :damselfly_files do |t|
4
+ t.string :file_uid
5
+ t.string :md5
6
+ t.integer :size
7
+ end
8
+
9
+ add_index :damselfly_files, [:md5, :size], unique: true
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ require 'damselfly/engine'
2
+
3
+ module Damselfly
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ has_many :attachments, as: :attachable, class_name: "Damselfly::Attachment"
8
+ end
9
+
10
+ module ClassMethods
11
+ def has_one_attached(name)
12
+ has_one name, as: :attachable, class_name: "Damselfly::Attachment", conditions: ["#{Damselfly::Attachment.table_name}.association_name = ?", name]
13
+ accepts_nested_attributes_for name
14
+
15
+ class_eval do
16
+ define_method("build_#{name}") do |args, opts|
17
+ super(args.merge!(association_name: name), opts)
18
+ end
19
+ end
20
+ end
21
+
22
+ def has_many_attached(name)
23
+ has_many name, as: :attachable, class_name: "Damselfly::Attachment", conditions: ["#{Damselfly::Attachment.table_name}.association_name = ?", name], before_add: lambda { |attachable,attachment| attachment.association_name = name }
24
+ accepts_nested_attributes_for name
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ module Damselfly
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Damselfly
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Damselfly
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :damselfly do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: damselfly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rodrigo Alvarez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-06 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: 3.2.13
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.13
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: database_cleaner
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
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: spork
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-spork
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Rails engine to add polymorphic file attachments leveraging dragonfly
98
+ and counting references to avoid duplicates..
99
+ email:
100
+ - papipo@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - app/models/damselfly/attachment.rb
106
+ - app/models/damselfly/file.rb
107
+ - config/routes.rb
108
+ - db/migrate/20130506093110_create_damselfly_attachments.rb
109
+ - db/migrate/20130506093950_create_damselfly_files.rb
110
+ - lib/damselfly/engine.rb
111
+ - lib/damselfly/version.rb
112
+ - lib/damselfly.rb
113
+ - lib/tasks/damselfly_tasks.rake
114
+ - MIT-LICENSE
115
+ - Rakefile
116
+ - README.rdoc
117
+ homepage: https://github.com/Papipo/damselfly
118
+ licenses: []
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.0.3
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Rails engine to add polymorphic file attachments leveraging dragonfly and
140
+ counting references to avoid duplicates.
141
+ test_files: []