nifty-attachments 1.0.3 → 1.0.4

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f7b25b580034be6e522ea66e3cdc92eeac58f52b
4
+ data.tar.gz: 5d627c81f06b214d050176989930f7f330468737
5
+ SHA512:
6
+ metadata.gz: d7b326a9633561b272183f416839e44229a0fa969f777d847c42960a31143f23b9431dc132a74e0541ecd12b3215cfce5eec20b15c4cc3d2bfd9f30d989f3ae5
7
+ data.tar.gz: 03e4810081627de43bc32fcdda4cb167265f8afe9486497665edcc3b9a27102d86a5cf9df79c07e18c95e0d8822dacd63c19e3bbe66c137ec8a7ab63ba909bc6
@@ -1,27 +1,27 @@
1
1
  module Nifty
2
2
  module Attachments
3
3
  class Attachment < ActiveRecord::Base
4
-
4
+
5
5
  # Set the table name
6
6
  self.table_name = 'nifty_attachments'
7
-
7
+
8
8
  # This will be the ActionDispatch::UploadedFile object which be diseminated
9
9
  # by the class on save.
10
10
  attr_accessor :uploaded_file
11
-
11
+
12
12
  # Relationships
13
13
  belongs_to :parent, :polymorphic => true
14
-
14
+
15
15
  # Validations
16
16
  validates :file_name, :presence => true
17
17
  validates :file_type, :presence => true
18
18
  validates :data, :presence => true
19
19
  validates :digest, :presence => true
20
20
  validates :token, :presence => true, :uniqueness => true
21
-
21
+
22
22
  # All attachments should have a token assigned to this
23
23
  before_validation { self.token = SecureRandom.uuid if self.token.blank? }
24
-
24
+
25
25
  # Copy values from the `uploaded_file` and set them as the appropriate
26
26
  # fields on this model
27
27
  before_validation do
@@ -32,22 +32,22 @@ module Nifty
32
32
  self.digest = Digest::MD5.hexdigest(self.data)
33
33
  end
34
34
  end
35
-
35
+
36
36
  # Return the attachment for a given role
37
37
  def self.for(role)
38
38
  self.where(:role => role).first
39
39
  end
40
-
40
+
41
41
  # Return the path to the attachment
42
42
  def path
43
43
  "/attachment/#{token}/#{file_name}"
44
44
  end
45
-
45
+
46
46
  # Is the attachment an image?
47
47
  def image?
48
48
  file_type =~ /\Aimage\//
49
49
  end
50
-
50
+
51
51
  end
52
52
  end
53
53
  end
@@ -1,11 +1,11 @@
1
1
  module Nifty
2
2
  module Attachments
3
3
  class Middleware
4
-
4
+
5
5
  def initialize(app)
6
6
  @app = app
7
7
  end
8
-
8
+
9
9
  def call(env)
10
10
  if env['PATH_INFO'] =~ /\A\/attachment\/([a-f0-9\-]{36})\/(.*)/
11
11
  if attachment = Nifty::Attachments::Attachment.find_by_token($1)
@@ -23,7 +23,7 @@ module Nifty
23
23
  @app.call(env)
24
24
  end
25
25
  end
26
-
26
+
27
27
  end
28
28
  end
29
29
  end
@@ -1,5 +1,5 @@
1
1
  class CreateNiftyAttachmentsTable < ActiveRecord::Migration
2
-
2
+
3
3
  def up
4
4
  create_table :nifty_attachments do |t|
5
5
  t.integer :parent_id
@@ -8,9 +8,9 @@ class CreateNiftyAttachmentsTable < ActiveRecord::Migration
8
8
  t.timestamps
9
9
  end
10
10
  end
11
-
11
+
12
12
  def down
13
13
  drop_table :nifty_attachments
14
14
  end
15
-
16
- end
15
+
16
+ end
@@ -5,17 +5,17 @@ module Nifty
5
5
  module Attachments
6
6
  class MigrationGenerator < Rails::Generators::Base
7
7
  include Rails::Generators::Migration
8
-
8
+
9
9
  source_root File.expand_path("../", __FILE__)
10
-
10
+
11
11
  def self.next_migration_number(path)
12
12
  Time.now.utc.strftime("%Y%m%d%H%M%S")
13
13
  end
14
-
14
+
15
15
  def create_model_file
16
16
  migration_template 'migration.rb', "db/migrate/create_nifty_attachments_table.rb"
17
17
  end
18
-
18
+
19
19
  end
20
20
  end
21
21
  end
@@ -5,6 +5,10 @@ module Nifty
5
5
  def self.included(base)
6
6
  base.extend ClassMethods
7
7
  base.after_save do
8
+ if @pending_attachment_deletions
9
+ self.nifty_attachments.where(:role => @pending_attachment_deletions).destroy_all
10
+ end
11
+
8
12
  if @pending_attachments
9
13
  @pending_attachments.each do |pa|
10
14
  old_attachments = self.nifty_attachments.where(:role => pa[:role]).pluck(:id)
@@ -17,12 +21,12 @@ module Nifty
17
21
  end
18
22
 
19
23
  module ClassMethods
20
-
24
+
21
25
  def attachment(name)
22
26
  unless self.reflect_on_all_associations(:has_many).map(&:name).include?(:nifty_attachments)
23
27
  has_many :nifty_attachments, :as => :parent, :dependent => :destroy, :class_name => 'Nifty::Attachments::Attachment'
24
28
  end
25
-
29
+
26
30
  has_one name, -> { select(:id, :token, :digest, :parent_id, :parent_type, :file_name, :file_type).where(:role => name) }, :class_name => 'Nifty::Attachments::Attachment', :as => :parent
27
31
 
28
32
  define_method "#{name}_file" do
@@ -38,8 +42,20 @@ module Nifty
38
42
  nil
39
43
  end
40
44
  end
45
+
46
+ define_method "#{name}_delete" do
47
+ instance_variable_get("@#{name}_delete")
48
+ end
49
+
50
+ define_method "#{name}_delete=" do |delete|
51
+ instance_variable_set("@#{name}_delete", delete)
52
+ unless delete.blank?
53
+ @pending_attachment_deletions ||= []
54
+ @pending_attachment_deletions << name
55
+ end
56
+ end
41
57
  end
42
-
58
+
43
59
  end
44
60
 
45
61
  end
@@ -1,24 +1,24 @@
1
1
  module Nifty
2
2
  module Attachments
3
3
  class Railtie < Rails::Railtie #:nodoc:
4
-
4
+
5
5
  initializer 'nifty.attachments.initialize' do |app|
6
-
6
+
7
7
  require 'nifty/attachments/middleware'
8
8
  app.config.middleware.use Nifty::Attachments::Middleware
9
-
9
+
10
10
  ActiveSupport.on_load(:active_record) do
11
11
  require 'nifty/attachments/attachment'
12
12
  require 'nifty/attachments/model_extension'
13
13
  ::ActiveRecord::Base.send :include, Nifty::Attachments::ModelExtension
14
14
  end
15
-
15
+
16
16
  end
17
-
17
+
18
18
  generators do
19
19
  require 'nifty/attachments/migration_generator'
20
20
  end
21
-
21
+
22
22
  end
23
23
  end
24
24
  end
@@ -1,5 +1,5 @@
1
1
  module Nifty
2
2
  module Attachments
3
- VERSION = '1.0.3'
3
+ VERSION = '1.0.4'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nifty-attachments
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
5
- prerelease:
4
+ version: 1.0.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Adam Cooke
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-07-28 00:00:00.000000000 Z
11
+ date: 2015-02-22 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Attach documents & files to Active Record models
15
14
  email:
@@ -18,6 +17,7 @@ executables: []
18
17
  extensions: []
19
18
  extra_rdoc_files: []
20
19
  files:
20
+ - lib/nifty/attachments.rb
21
21
  - lib/nifty/attachments/attachment.rb
22
22
  - lib/nifty/attachments/middleware.rb
23
23
  - lib/nifty/attachments/migration.rb
@@ -25,30 +25,27 @@ files:
25
25
  - lib/nifty/attachments/model_extension.rb
26
26
  - lib/nifty/attachments/railtie.rb
27
27
  - lib/nifty/attachments/version.rb
28
- - lib/nifty/attachments.rb
29
28
  homepage: https://github.com/niftyware/attachments
30
29
  licenses: []
30
+ metadata: {}
31
31
  post_install_message:
32
32
  rdoc_options: []
33
33
  require_paths:
34
34
  - lib
35
35
  required_ruby_version: !ruby/object:Gem::Requirement
36
- none: false
37
36
  requirements:
38
- - - ! '>='
37
+ - - ">="
39
38
  - !ruby/object:Gem::Version
40
39
  version: '0'
41
40
  required_rubygems_version: !ruby/object:Gem::Requirement
42
- none: false
43
41
  requirements:
44
- - - ! '>='
42
+ - - ">="
45
43
  - !ruby/object:Gem::Version
46
44
  version: '0'
47
45
  requirements: []
48
46
  rubyforge_project:
49
- rubygems_version: 1.8.23.2
47
+ rubygems_version: 2.2.2
50
48
  signing_key:
51
- specification_version: 3
49
+ specification_version: 4
52
50
  summary: Attach documents & files to Active Record models
53
51
  test_files: []
54
- has_rdoc: