has_images 0.0.1

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.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ Digineo HasImages gem
2
+ =========
3
+
4
+ ## Description
5
+ HasImages adds images and galleries to your ActiveRecord models.
6
+ It comes with the follwing mehtods:
7
+
8
+ model.images # returns a list of all images
9
+ model.avatar # returns the image with the avatar flag e.g. the first image
10
+ model.galleries? # does this model have a gallery?
11
+ model.galleries # returns a list of the models image galleries
12
+ gallery.images # returns all images in a gallery
13
+ image.set_avatar # sets the avatar flag on this image
14
+
15
+ ## Dependencies
16
+ This gem requires thoughtbot's [paperclip](http://github.com/thoughtbot/paperclip), this gem will be installed automatically.
17
+
18
+ ## Installation:
19
+ add the following to your config/enviroment.rb
20
+
21
+ config.gem "has_images"
22
+
23
+
24
+ ## Generate migrations & migrate
25
+
26
+ ./script/generate has_images
27
+
28
+ rake db:migrate
29
+
30
+ ## Integration in your model
31
+
32
+ class User < ActiveRecord::Base
33
+ has_images
34
+ end
35
+
36
+ ## Examples & usage
37
+
38
+ ### Create image by URL
39
+
40
+ image = user.create_image_by_url("http://image.url/file.png")
41
+
42
+ ### Images with galleries
43
+
44
+ gallery = user.galleries.create(:name => "gallery name")
45
+
46
+ gallery.images.create(params[:image])
47
+ gallery.create_image_by_url("http://image.url/file.png")
48
+
49
+ ### Imageupload via form
50
+
51
+
52
+
53
+
54
+ Copyright (c) 2010 Dennis Meise [Digineo GmbH](http://www.digineo.de/ "Digineo GmbH") , released under the MIT license
55
+
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the has_images plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the has_images plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'HasImages'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Creates migrations for Digineo::ImageType, Digineo::ImageGallery and Digineo::Image
3
+
4
+ Example:
5
+ ./script/generate has_images
6
+
7
+ This will create:
8
+ db/migrate/...create_has_images.rb
@@ -0,0 +1,7 @@
1
+ class HasImagesGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template 'db/migrate/create_has_images.rb', "db/migrate", { :migration_file_name => "create_has_images" }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,38 @@
1
+ class CreateHasImages < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :digineo_image_types do |t|
4
+ t.string :name
5
+ end
6
+
7
+ create_table :digineo_image_galleries do |t|
8
+ t.string :name
9
+ t.integer :parentmodel_id, :references => nil
10
+ t.string :parentmodel_type
11
+ t.timestamps
12
+ end
13
+
14
+
15
+ create_table :digineo_images do |t|
16
+ t.string :name
17
+ t.text :text
18
+ t.integer :gallery_id, :on_delete => :set_null, :references => :digineo_image_galleries
19
+ t.integer :image_type_id, :on_delete => :set_null, :references => :digineo_image_types
20
+ t.integer :parentmodel_id, :references => nil
21
+ t.string :parentmodel_type
22
+ t.string :file_file_name
23
+ t.string :file_content_type
24
+ t.string :file_remote_url
25
+ t.integer :file_file_size
26
+ t.datetime :file_updated_at
27
+ t.string :type
28
+ t.boolean :avatar, :default => false, :null => false
29
+ t.timestamps
30
+ end
31
+ end
32
+
33
+ def self.down
34
+ drop_table :digineo_images
35
+ drop_table :digineo_image_galleries
36
+ drop_table :digineo_image_types
37
+ end
38
+ end
data/init.rb ADDED
@@ -0,0 +1,16 @@
1
+ # Include hook code here
2
+ require 'open-uri'
3
+ require 'has_images'
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.seo_name
16
+ end
@@ -0,0 +1,70 @@
1
+ class Digineo::Image < ActiveRecord::Base
2
+
3
+ set_table_name :digineo_images
4
+
5
+ belongs_to :parentmodel, :polymorphic => true
6
+ belongs_to :gallery, :class_name => "Digineo::ImageGallery"
7
+ belongs_to :image_type, :class_name => "Digineo::ImageType"
8
+ attr_accessor :file_url
9
+
10
+ before_create :should_be_avatar?
11
+ before_destroy :unset_avatar if :avatar
12
+
13
+ named_scope :not_avatar, :conditions => "avatar=0"
14
+ named_scope :without_gallery, :conditions => "gallery_id IS NULL"
15
+
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"
26
+
27
+
28
+ validates_attachment_presence :file, :unless => :file_url_provided?
29
+ validates_presence_of :parentmodel
30
+ before_validation :download_remote_file, :if => :file_url_provided?
31
+
32
+ validates_presence_of :file_remote_url, :if => :file_url_provided?, :message => 'is invalid or inaccessible'
33
+
34
+ def set_avatar
35
+ parentmodel.avatar.unset_avatar if parentmodel.has_avatar?
36
+ update_attribute(:avatar, true)
37
+ end
38
+
39
+ def unset_avatar
40
+ update_attribute(:avatar, false)
41
+ end
42
+
43
+ private
44
+
45
+ def should_be_avatar?
46
+ self.avatar = !parentmodel.avatar
47
+ true # returns true because it's called by before_create
48
+ end
49
+
50
+ def file_url_provided?
51
+ !self.file_url.blank?
52
+ end
53
+
54
+ def file_url_provided?
55
+ !self.file_url.blank?
56
+ end
57
+
58
+ def download_remote_file
59
+ self.file = do_download_remote_file
60
+ self.file_remote_url = file_url
61
+ end
62
+
63
+ def do_download_remote_file
64
+ io = open(URI.parse(file_url))
65
+ def io.original_filename; base_uri.path.split('/').last; end
66
+ io.original_filename.blank? ? nil : io
67
+ rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
68
+ end
69
+
70
+ end
@@ -0,0 +1,15 @@
1
+ class Digineo::ImageGallery < ActiveRecord::Base
2
+
3
+ set_table_name :digineo_image_galleries
4
+
5
+ belongs_to :parentmodel, :polymorphic => true
6
+ has_many :images, :class_name => 'Digineo::Image', :foreign_key => :gallery_id
7
+
8
+ validates_presence_of :name
9
+ validates_uniqueness_of :name, :scope => [:parentmodel_id, :parentmodel_type]
10
+
11
+ def create_image_by_url(url, image_type = nil)
12
+ images.create!(:file_url => url, :parentmodel => parentmodel, :image_type => image_type)
13
+ end
14
+
15
+ end
@@ -0,0 +1,9 @@
1
+ class Digineo::ImageType < ActiveRecord::Base
2
+ set_table_name :digineo_image_types
3
+
4
+ has_many :images, :class_name => "Digineo::Image"
5
+
6
+ validates_presence_of :name
7
+ validates_uniqueness_of :name
8
+
9
+ end
data/lib/has_images.rb ADDED
@@ -0,0 +1,40 @@
1
+ # HasImages
2
+ module HasImages
3
+ def self.included(base)
4
+ base.send :extend, ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ # adds has_images to model
9
+ def has_images
10
+ has_many :images, :as => :parentmodel, :dependent => :destroy, :order => 'id ASC', :class_name => "Digineo::Image"
11
+ 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
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+
19
+ ## gibt alle Bilder zurück die nicht avatar sind
20
+ def more_images
21
+ images.not_avatar
22
+ end
23
+
24
+ def images_without_gallery
25
+ images.without_gallery
26
+ end
27
+
28
+ def has_more_images?
29
+ images.not_avatar.any?
30
+ end
31
+
32
+ def galleries?
33
+ galleries.any?
34
+ end
35
+
36
+ def create_image_by_url(url, image_type = nil)
37
+ images.create!(:file_url => url, :image_type => image_type)
38
+ end
39
+ end
40
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "..", "init")
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class Digineo::ImageGalleryTest < ActiveSupport::TestCase
4
+ should_validate_presence_of :name
5
+ should_belong_to :parentmodel
6
+ should_have_many :images
7
+ should_validate_uniqueness_of :name, :scoped_to => [:parentmodel_id, :parentmodel_type]
8
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class Digineo::ImageTest < ActiveSupport::TestCase
4
+
5
+ should_belong_to :parentmodel, :gallery, :image_type
6
+
7
+ end
@@ -0,0 +1,6 @@
1
+ require 'test_helper'
2
+
3
+ class Digineo::ImageTypeTest < ActiveSupport::TestCase
4
+ should_validate_presence_of :name
5
+ should_have_many :images
6
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class HasImagesTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_images
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dennis Meise
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-17 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: paperclip
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2.3"
24
+ version:
25
+ description:
26
+ email: github@digineo.de
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.md
33
+ files:
34
+ - generators/has_images/has_images_generator.rb
35
+ - generators/has_images/USAGE
36
+ - generators/has_images/templates/db/migrate/create_has_images.rb
37
+ - init.rb
38
+ - Rakefile
39
+ - README.md
40
+ - lib/has_images.rb
41
+ - lib/digineo/image_gallery.rb
42
+ - lib/digineo/image_type.rb
43
+ - lib/digineo/image.rb
44
+ - rails/init.rb
45
+ has_rdoc: true
46
+ homepage: http://www.digineo.de
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --inline-source
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: HasImages adds images and galleries to your ActiveRecord models.
74
+ test_files:
75
+ - test/digineo/image_gallery_test.rb
76
+ - test/digineo/image_type_test.rb
77
+ - test/digineo/image_test.rb
78
+ - test/has_images_test.rb
79
+ - test/test_helper.rb