has_images 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -32,6 +32,20 @@ add the following to your config/enviroment.rb
32
32
  class User < ActiveRecord::Base
33
33
  has_images
34
34
  end
35
+
36
+ you can also customize the image-storage with paperclip options
37
+
38
+ class User < ActiveRecord::Base
39
+ has_images :styles => {
40
+ :thumb => ["150x100", :jpg],
41
+ :mini => ["75x50", :jpg],
42
+ :medium => ["300x200", :jpg],
43
+ :large => ["640x480", :jpg],
44
+ :huge => ["800x600", :jpg],
45
+ :square => ["200x200", :jpg] },
46
+ :path => ":rails_root/public/images/:attachment/:id_partition/:id_:style.:extension",
47
+ :url => "/images/:attachment/:id_partition/:id_:style.:extension"
48
+ end
35
49
 
36
50
  ## Examples & usage
37
51
 
data/init.rb CHANGED
@@ -2,15 +2,3 @@
2
2
  require 'open-uri'
3
3
  require 'has_images'
4
4
  ActiveRecord::Base.send(:include, HasImages)
5
-
6
- Paperclip.interpolates :short_id_partition do |attachment, style|
7
- ("%08d" % attachment.instance.id).scan(/\d{4}/).join("/")
8
- end
9
-
10
- Paperclip.interpolates :parent do |attachment, style|
11
- attachment.instance.parentmodel.image_folder rescue attachment.instance.parentmodel.class.to_s.underscore.pluralize
12
- end
13
-
14
- Paperclip.interpolates :parent_name do |attachment, style|
15
- attachment.instance.parentmodel.friendly_name rescue attachment.instance.parentmodel.to_s
16
- end
data/lib/digineo/image.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  class Digineo::Image < ActiveRecord::Base
2
2
 
3
3
  set_table_name :digineo_images
4
+ store_full_sti_class = true
4
5
 
5
6
  belongs_to :parentmodel, :polymorphic => true
6
7
  belongs_to :gallery, :class_name => "Digineo::ImageGallery"
@@ -13,16 +14,7 @@ class Digineo::Image < ActiveRecord::Base
13
14
  named_scope :not_avatar, :conditions => "avatar=0"
14
15
  named_scope :without_gallery, :conditions => "gallery_id IS NULL"
15
16
 
16
-
17
- has_attached_file :file, :styles => {
18
- :thumb => ["150x100", :jpg],
19
- :mini => ["75x50", :jpg],
20
- :medium => ["300x200", :jpg],
21
- :large => ["640x480", :jpg],
22
- :huge => ["800x600", :jpg],
23
- :square => ["200x200", :jpg] },
24
- :path => ":rails_root/public/images/:parent/:short_id_partition/:parent_name_:style.:extension",
25
- :url => "/images/:parent/:short_id_partition/:parent_name_:style.:extension"
17
+ has_attached_file :file
26
18
 
27
19
 
28
20
  validates_attachment_presence :file, :unless => :file_url_provided?
@@ -32,7 +24,7 @@ class Digineo::Image < ActiveRecord::Base
32
24
  validates_presence_of :file_remote_url, :if => :file_url_provided?, :message => 'is invalid or inaccessible'
33
25
 
34
26
  def set_avatar
35
- parentmodel.avatar.unset_avatar if parentmodel.has_avatar?
27
+ parentmodel.avatar.unset_avatar if parentmodel.avatar
36
28
  update_attribute(:avatar, true)
37
29
  end
38
30
 
data/lib/digineo.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Digineo
2
+
3
+ end
data/lib/has_images.rb CHANGED
@@ -1,16 +1,31 @@
1
1
  # HasImages
2
+
2
3
  module HasImages
3
4
  def self.included(base)
4
- base.send :extend, ClassMethods
5
+ base.send :extend, ClassMethods
5
6
  end
6
7
 
7
8
  module ClassMethods
8
9
  # adds has_images to model
9
- def has_images
10
- has_many :images, :as => :parentmodel, :dependent => :destroy, :order => 'id ASC', :class_name => "Digineo::Image"
10
+ def has_images(options={})
11
+
12
+ # eval is not always evil ;)
13
+ # we generate a Digineo::Model::Image clase to store the given paperclip configuration in it
14
+ eval <<-EOF
15
+ module Digineo::#{self.class_name}
16
+ class Digineo::#{self.class_name}::Image < Digineo::Image
17
+ has_attached_file :file, #{options.inspect}
18
+ end
19
+ end
20
+ EOF
21
+
22
+ has_many :images, :as => :parentmodel, :dependent => :destroy, :order => 'id ASC', :class_name => "Digineo::#{self.class_name}::Image"
11
23
  has_one :avatar, :as => :parentmodel, :order => 'id ASC', :class_name => "Digineo::Image", :conditions => 'avatar=1'
12
- has_many :galleries, :as => :parentmodel, :dependent => :destroy, :class_name => 'Digineo::ImageGallery'
13
- send :include, InstanceMethods
24
+ has_many :galleries, :as => :parentmodel, :dependent => :destroy, :class_name => 'Digineo::ImageGallery'
25
+
26
+ named_scope :with_avatar, :include => :avatar
27
+
28
+ send :include, InstanceMethods
14
29
  end
15
30
  end
16
31
 
@@ -1,4 +1,34 @@
1
- require 'test_helper'
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class User < ActiveRecord::Base
4
+ has_images :styles => {
5
+ :thumb => ["150x100", :jpg],
6
+ :mini => ["75x50", :jpg],
7
+ :medium => ["300x200", :jpg],
8
+ :large => ["640x480", :jpg],
9
+ :huge => ["800x600", :jpg],
10
+ :square => ["200x200", :jpg] },
11
+ :path => ":rails_root/public/images/:parent/:short_id_partition/:parent_name_:style.:extension",
12
+ :url => "/images/:parent/:short_id_partition/:parent_name_:style.:extension"
13
+ end
14
+
15
+ class UserTest < ActiveSupport::TestCase
16
+ context "user model" do
17
+ should_have_many :images, :galleries
18
+ should_have_named_scope :with_avatar
19
+
20
+
21
+ context "image configuration" do
22
+ should "exist class Digineo::User::Image" do
23
+ user = User.new
24
+
25
+ assert Digineo::User::Image
26
+
27
+
28
+ end
29
+ end
30
+ end
31
+ end
2
32
 
3
33
  class HasImagesTest < ActiveSupport::TestCase
4
34
  # Replace this with your real tests.
data/test/test_helper.rb CHANGED
@@ -1,3 +1,52 @@
1
1
  require 'rubygems'
2
- require 'active_support'
3
- require 'active_support/test_case'
2
+ require 'active_record'
3
+ require 'shoulda/rails'
4
+ require 'active_support/test_case'
5
+ require 'paperclip'
6
+
7
+ require File.expand_path(File.dirname(__FILE__)+"/../lib/digineo")
8
+ require File.expand_path(File.dirname(__FILE__)+"/../lib/digineo/image")
9
+ require File.expand_path(File.dirname(__FILE__)+"/../lib/digineo/image_gallery")
10
+ require File.expand_path(File.dirname(__FILE__)+"/../lib/digineo/image_type")
11
+ require File.expand_path(File.dirname(__FILE__)+"/../lib/has_images")
12
+ ActiveRecord::Base.send(:include, HasImages)
13
+
14
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
15
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
16
+ ActiveRecord::Base.establish_connection(config['test'])
17
+
18
+ ActiveRecord::Schema.define do
19
+ create_table "users", :force => true do |t|
20
+ t.column "name", :text
21
+ t.column "email", :text
22
+ end
23
+
24
+ create_table :digineo_image_types do |t|
25
+ t.string :name
26
+ end
27
+
28
+ create_table :digineo_image_galleries do |t|
29
+ t.string :name
30
+ t.integer :parentmodel_id, :references => nil
31
+ t.string :parentmodel_type
32
+ t.timestamps
33
+ end
34
+
35
+ create_table :digineo_images do |t|
36
+ t.string :name
37
+ t.text :text
38
+ t.integer :gallery_id, :on_delete => :set_null, :references => :digineo_image_galleries
39
+ t.integer :image_type_id, :on_delete => :set_null, :references => :digineo_image_types
40
+ t.integer :parentmodel_id, :references => nil
41
+ t.string :parentmodel_type
42
+ t.string :file_file_name
43
+ t.string :file_content_type
44
+ t.string :file_remote_url
45
+ t.integer :file_file_size
46
+ t.datetime :file_updated_at
47
+ t.string :type
48
+ t.boolean :avatar, :default => false, :null => false
49
+ t.timestamps
50
+ end
51
+ end
52
+
@@ -1,8 +1,7 @@
1
- require 'test_helper'
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
2
 
3
3
  class Digineo::ImageGalleryTest < ActiveSupport::TestCase
4
4
  should_validate_presence_of :name
5
5
  should_belong_to :parentmodel
6
6
  should_have_many :images
7
- should_validate_uniqueness_of :name, :scoped_to => [:parentmodel_id, :parentmodel_type]
8
7
  end
@@ -1,4 +1,5 @@
1
- require 'test_helper'
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
2
3
 
3
4
  class Digineo::ImageTest < ActiveSupport::TestCase
4
5
 
@@ -1,4 +1,4 @@
1
- require 'test_helper'
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
2
 
3
3
  class Digineo::ImageTypeTest < ActiveSupport::TestCase
4
4
  should_validate_presence_of :name
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_images
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Meise
@@ -38,6 +38,7 @@ files:
38
38
  - Rakefile
39
39
  - README.md
40
40
  - lib/has_images.rb
41
+ - lib/digineo.rb
41
42
  - lib/digineo/image_gallery.rb
42
43
  - lib/digineo/image_type.rb
43
44
  - lib/digineo/image.rb
@@ -72,8 +73,8 @@ signing_key:
72
73
  specification_version: 3
73
74
  summary: HasImages adds images and galleries to your ActiveRecord models.
74
75
  test_files:
75
- - test/digineo/image_gallery_test.rb
76
- - test/digineo/image_type_test.rb
77
- - test/digineo/image_test.rb
76
+ - test/unit/digineo/image_gallery_test.rb
77
+ - test/unit/digineo/image_type_test.rb
78
+ - test/unit/digineo/image_test.rb
78
79
  - test/has_images_test.rb
79
80
  - test/test_helper.rb