has_images 0.1.94 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -49,20 +49,52 @@ add the following to your config/enviroment.rb
49
49
 
50
50
  ## Examples & usage
51
51
 
52
- ### Create image by URL
52
+ ### Imageupload via form
53
+
54
+ in your view (uses [formtastic](http://github.com/justinfrench/formtastic))
55
+
56
+ -semantic_form_for @image, :html => { :multipart => true } do |f|
57
+ -f.inputs do
58
+ =f.input :gallery_id, :as => :select, :collection => parent.galleries.all(:order=>'name')
59
+ =f.input :gallery_name
60
+ =f.input :name
61
+ =f.input :text
62
+ =f.file_field :file, :input_html => { :size => 22 }
63
+ =f.buttons
64
+
65
+ in your controller:
53
66
 
54
- image = user.create_image_by_url("http://image.url/file.png")
55
-
56
- ### Images with galleries
67
+ class ImagesController
57
68
 
58
- gallery = user.galleries.create(:name => "gallery name")
69
+ def new
70
+ @image = User.first.images.new
71
+ end
72
+
73
+ def create
74
+ .....
75
+ @image = User.first.images.create(params[:image])
76
+
77
+ .....
78
+ end
59
79
 
60
- gallery.images.create(params[:image])
61
- gallery.create_image_by_url("http://image.url/file.png")
80
+ end
62
81
 
63
- ### Imageupload via form
64
82
 
65
- ### enabling Counter-Cache (since version 0.1.7)
83
+ ### Create image by URL
84
+ has_images supports an easy method to create images from URLs:
85
+
86
+ user = User.first
87
+ image = user.create_image_by_url("http://image.url/file.png")
88
+
89
+ Of course you can also create the image manually with more arguments
90
+
91
+ user = User.first
92
+ user.images.create(:file_url => "http://image.url/file.png", :name => "my image")
93
+
94
+
95
+
96
+
97
+ ### enabling Counter-Cache
66
98
 
67
99
  class User < ActiveRecord::Base
68
100
  has_images :styles => {
@@ -75,6 +107,54 @@ add the following to your config/enviroment.rb
75
107
 
76
108
  This enables the counter_cache for the user-model. CounterCache is saved in the field "images_count"
77
109
 
110
+ ## Galleries
111
+
112
+ has_images also supports image galleries to group images
113
+
114
+ ### Images with galleries
115
+
116
+ gallery = user.galleries.create(:name => "gallery name")
117
+
118
+ gallery.images.create(params[:image])
119
+ gallery.create_image_by_url("http://image.url/file.png")
120
+
121
+ ### Create image gallery on the fly
122
+
123
+ You can also create image galleries on the fly with the attr_accessor gallery_name:
124
+
125
+ user = User.find(1)
126
+ image = user.images.create(:name => 'my cool image', :file_url => "http://image.url/image.jpg", :gallery_name => "my images")
127
+
128
+ ## Image Types
129
+
130
+ Sometimes you also have to group your images by specific image types. For example you could use the types "logo", "screenshot" and "banner"
131
+
132
+ ### creating image types:
133
+
134
+ You can easily create ImageType like this:
135
+
136
+ Digineo::ImageType.create(:name => 'logo')
137
+ Digineo::ImageType.create(:name => 'screenshot')
138
+ Digineo::ImageType.create(:name => 'banner')
139
+
140
+ another possibility is to create image types on the fly like this
141
+
142
+ user = User.first
143
+ user.images.create(:name => 'my cool image', :file_url => "http://image.url/image.jpg", :image_type_name => 'logo')
144
+
145
+ in this case the ImageType "logo" will eiter be created or the id of an existing ImageType will be set
146
+
147
+ ### finding images of a specific type
148
+
149
+ user = User.first
150
+ logos = user.images.image_type('logo')
151
+
152
+ invalid image types will raise an Digineo::ImageType::Exception
153
+
154
+ user = User.first
155
+ invalid = user.images.image_type('invalid image type')
156
+ => Digineo::ImageType::Exception: Could not find ImageType with name invalid image type
157
+
78
158
 
79
159
  Copyright (c) 2010 Dennis Meise [Digineo GmbH](http://www.digineo.de/ "Digineo GmbH") , released under the MIT license
80
160
 
data/lib/digineo/image.rb CHANGED
@@ -8,64 +8,80 @@ class Digineo::Image < ActiveRecord::Base
8
8
  belongs_to :image_type, :class_name => "Digineo::ImageType"
9
9
  attr_accessor :file_url
10
10
  attr_accessor :gallery_name
11
-
11
+ attr_accessor :image_type_name
12
+
12
13
  before_create :should_be_avatar?
13
14
  before_destroy :unset_avatar if :avatar
14
15
 
15
16
  named_scope :not_avatar, :conditions => "avatar=0"
16
17
  named_scope :without_gallery, :conditions => "gallery_id IS NULL"
17
18
 
18
- has_attached_file :file
19
-
20
- after_save :set_gallery
21
-
22
19
 
23
- validates_attachment_presence :file, :unless => :file_url_provided?, :on => :create
24
- validates_presence_of :parentmodel
25
- before_validation :download_remote_file, :if => :file_url_provided?
26
-
27
- validates_presence_of :file_remote_url, :if => :file_url_provided?, :message => 'is invalid or inaccessible'
28
-
29
- def set_avatar
30
- parentmodel.avatar.unset_avatar if parentmodel.avatar
31
- update_attribute(:avatar, true)
20
+ named_scope :image_type, lambda { |*types|
21
+ { :conditions => "image_type_id IN (" + types.collect do |type|
22
+ begin
23
+ Digineo::ImageType.find_by_name(type).id
24
+ rescue
25
+ raise Digineo::ImageType::Exception, "Could not find ImageType with name #{type}"
26
+ end
27
+ end.compact.join(",") + ")"}
28
+ }
29
+
30
+ has_attached_file :file
31
+
32
+ after_save :set_gallery, :set_image_type
33
+
34
+
35
+ validates_attachment_presence :file, :unless => :file_url_provided?, :on => :create
36
+ validates_presence_of :parentmodel
37
+ before_validation :download_remote_file, :if => :file_url_provided?
38
+
39
+ validates_presence_of :file_remote_url, :if => :file_url_provided?, :message => 'is invalid or inaccessible'
40
+
41
+ # sets the avatar flag on this image
42
+ def set_avatar
43
+ parentmodel.avatar.unset_avatar if parentmodel.avatar
44
+ update_attribute(:avatar, true)
45
+ end
46
+
47
+ # removes avatar flag
48
+ def unset_avatar
49
+ update_attribute(:avatar, false)
50
+ end
51
+
52
+ def set_gallery
53
+ return if gallery_id or gallery_name.to_s.empty?
54
+ self.gallery_id = parentmodel.find_or_create_gallery(gallery_name).id
55
+ save
56
+ end
57
+
58
+ def set_image_type
59
+ return if image_type_id or image_type_name.to_s.empty?
60
+ self.image_type_id = ImageType.find_or_create_by_name(image_type_name).id
61
+ save
62
+ end
63
+
64
+ private
65
+
66
+ def should_be_avatar?
67
+ self.avatar = !parentmodel.avatar
68
+ true # returns true because it's called by before_create
69
+ end
70
+
71
+ def file_url_provided?
72
+ !self.file_url.blank?
73
+ end
74
+
75
+ def download_remote_file
76
+ self.file = do_download_remote_file
77
+ self.file_remote_url = file_url
78
+ end
79
+
80
+ def do_download_remote_file
81
+ io = open(URI.parse(file_url))
82
+ def io.original_filename; base_uri.path.split('/').last; end
83
+ io.original_filename.blank? ? nil : io
84
+ rescue # TODO catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
85
+ end
86
+
32
87
  end
33
-
34
- def unset_avatar
35
- update_attribute(:avatar, false)
36
- end
37
-
38
- def set_gallery
39
- return if gallery_id or gallery_name.to_s.empty?
40
- self.gallery_id = parentmodel.find_or_create_gallery(gallery_name).id
41
- save
42
- end
43
-
44
- private
45
-
46
- def should_be_avatar?
47
- self.avatar = !parentmodel.avatar
48
- true # returns true because it's called by before_create
49
- end
50
-
51
- def file_url_provided?
52
- !self.file_url.blank?
53
- end
54
-
55
- def file_url_provided?
56
- !self.file_url.blank?
57
- end
58
-
59
- def download_remote_file
60
- self.file = do_download_remote_file
61
- self.file_remote_url = file_url
62
- end
63
-
64
- def do_download_remote_file
65
- io = open(URI.parse(file_url))
66
- def io.original_filename; base_uri.path.split('/').last; end
67
- io.original_filename.blank? ? nil : io
68
- rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
69
- end
70
-
71
- end
@@ -7,3 +7,8 @@ class Digineo::ImageType < ActiveRecord::Base
7
7
  validates_uniqueness_of :name
8
8
 
9
9
  end
10
+
11
+
12
+ class Digineo::ImageType::Exception < Exception
13
+
14
+ end
data/lib/has_images.rb CHANGED
@@ -8,7 +8,7 @@ module HasImages
8
8
  module ClassMethods
9
9
  # adds has_images to model
10
10
  def has_images(options={})
11
- counter_cache = options.delete(:couter_cache) || false
11
+ counter_cache = options.delete(:counter_cache) || false
12
12
  # eval is not always evil ;)
13
13
  # we generate a Digineo::Model::Image clase to store the given paperclip configuration in it
14
14
  eval <<-EOF
@@ -32,15 +32,17 @@ module HasImages
32
32
 
33
33
  module InstanceMethods
34
34
 
35
- ## gibt alle Bilder zurück die nicht avatar sind
35
+ # returns all images that are not set as avatar
36
36
  def more_images
37
37
  images.not_avatar
38
38
  end
39
39
 
40
+ # returns all images without gallery
40
41
  def images_without_gallery
41
42
  images.without_gallery
42
43
  end
43
44
 
45
+ # does this model have any images, that are not set as avatar?
44
46
  def has_more_images?
45
47
  images.not_avatar.any?
46
48
  end
@@ -53,6 +55,7 @@ module HasImages
53
55
  galleries.find_or_create_by_name(name)
54
56
  end
55
57
 
58
+
56
59
  def create_image_by_url(url, image_type = nil)
57
60
  images.create!(:file_url => url, :image_type => image_type)
58
61
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_images
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.94
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ version: "0.2"
5
10
  platform: ruby
6
11
  authors:
7
12
  - Dennis Meise
@@ -9,19 +14,24 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-08-06 00:00:00 +02:00
17
+ date: 2010-08-23 00:00:00 +02:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: paperclip
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
20
25
  requirements:
21
26
  - - ">="
22
27
  - !ruby/object:Gem::Version
28
+ hash: 5
29
+ segments:
30
+ - 2
31
+ - 3
23
32
  version: "2.3"
24
- version:
33
+ type: :runtime
34
+ version_requirements: *id001
25
35
  description:
26
36
  email: github@digineo.de
27
37
  executables: []
@@ -43,6 +53,11 @@ files:
43
53
  - lib/digineo/image_type.rb
44
54
  - lib/digineo/image.rb
45
55
  - rails/init.rb
56
+ - test/unit/digineo/image_gallery_test.rb
57
+ - test/unit/digineo/image_type_test.rb
58
+ - test/unit/digineo/image_test.rb
59
+ - test/has_images_test.rb
60
+ - test/test_helper.rb
46
61
  has_rdoc: true
47
62
  homepage: http://github.com/digineo/has_images
48
63
  licenses: []
@@ -54,21 +69,27 @@ rdoc_options:
54
69
  require_paths:
55
70
  - lib
56
71
  required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
57
73
  requirements:
58
74
  - - ">="
59
75
  - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
60
79
  version: "0"
61
- version:
62
80
  required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
63
82
  requirements:
64
83
  - - ">="
65
84
  - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
66
88
  version: "0"
67
- version:
68
89
  requirements: []
69
90
 
70
91
  rubyforge_project:
71
- rubygems_version: 1.3.5
92
+ rubygems_version: 1.3.7
72
93
  signing_key:
73
94
  specification_version: 3
74
95
  summary: HasImages adds images and galleries to your ActiveRecord models.