has_magick_title 0.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ .DS_Store
4
+ Gemfile.lock
5
+ pkg/*
6
+ test/dummy
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Redistribution and use in source and binary forms, with or without modification,
2
+ are permitted provided that the following conditions are met:
3
+
4
+ * Redistributions of source code must retain the above copyright notice,
5
+ this list of conditions and the following disclaimer.
6
+ * Redistributions in binary form must reproduce the above copyright notice,
7
+ this list of conditions and the following disclaimer in the documentation
8
+ and/or other materials provided with the distribution.
9
+ * Neither the name of the Rails Dog LLC nor the names of its
10
+ contributors may be used to endorse or promote products derived from this
11
+ software without specific prior written permission.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ Has Magick Title
2
+ ================
3
+
4
+ Installation
5
+ ------------
6
+
7
+ Since has_magick_title is to be used with rails, we'll install it by adding the following to your Gemfile and running the `bundle` command.
8
+
9
+ gem 'has_magick_title', :git => 'git://github.com/citrus/has_magick_title.git'
10
+
11
+ Now bundle up:
12
+
13
+ bundle
14
+
15
+
16
+ Usage
17
+ -----
18
+
19
+ To automagically generate your model's title attribute into an image, just call the `has_magick_title` class method.
20
+
21
+ # assume the defaults (:attribute => :title)
22
+ class Post < ActiveRecord::Base
23
+ has_magick_title
24
+ end
25
+
26
+
27
+ # custom attribute and magick_title options
28
+ class Person < ActiveRecord::Base
29
+ has_magick_title :name,
30
+ :color => "#e00053",
31
+ :font_size => 19
32
+ end
33
+
34
+ # custom attribute and magick_title style
35
+
36
+ MagickTitle.style :h1 do
37
+ font_size 50
38
+ line_height -10
39
+ end
40
+
41
+ class Person < ActiveRecord::Base
42
+ has_magick_title :name, :h1
43
+ end
44
+
45
+
46
+ ### See [magick_title](https://github.com/citrus/magick_title) for more...
47
+
48
+
49
+ Testing
50
+ -------
51
+
52
+ Testing for has_magick_title is done with [shoulda](https://github.com/thoughtbot/shoulda), [spork](https://github.com/timcharper/spork) and [dummier](https://github.com/citrus/dummier).
53
+
54
+ Start by cloning the project:
55
+
56
+ git clone git://github.com/citrus/has_magick_title.git
57
+ cd has_magick_title
58
+
59
+
60
+ To get your environment setup, just run the following:
61
+
62
+ bundle
63
+ bundle exec dummier
64
+
65
+
66
+ Now run the unit tests with rake:
67
+
68
+ rake
69
+
70
+
71
+ If you'd like to spork, you probably already know what to do. Otherwise, skip the rake step and run spork instead:
72
+
73
+ spork
74
+
75
+
76
+ Now in another window:
77
+
78
+ cd /to/where/you/cloned/has_magick_title
79
+ testdrb test/unit/*.rb
80
+
81
+
82
+ License
83
+ -------
84
+
85
+ Copyright (c) 2011 Spencer Steffen and Citrus, released under the New BSD License All rights reserved.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'lib'
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ t.verbose = false
11
+ end
12
+
13
+ desc "Default Task"
14
+ task :default => [ :test ]
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "has_magick_title/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "has_magick_title"
7
+ s.version = HasMagickTitle::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Spencer Steffen"]
10
+ s.email = ["spencer@citrusme.com"]
11
+ s.homepage = "https://github.com/citrus/has_magick_title"
12
+ s.summary = %q{Automagically creates an image of your rails model's title using magick_title.}
13
+ s.description = %q{Automagically creates an image of your rails model's title using magick_title.}
14
+
15
+ s.rubyforge_project = "has_magick_title"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'magick_title', '>= 0.2.0.rc1'
23
+
24
+ s.add_development_dependency 'rails', '>= 3.0.0'
25
+ s.add_development_dependency 'sqlite3', '>= 1.3.3'
26
+ s.add_development_dependency 'shoulda', '2.11.3'
27
+ s.add_development_dependency 'dummier', '0.1.0'
28
+ s.add_development_dependency('spork', '>= 0.9.0.rc7')
29
+ s.add_development_dependency('spork-testunit', '>= 0.0.5')
30
+
31
+ end
@@ -0,0 +1,20 @@
1
+ prepend_file 'config/application.rb', %(require "rake"
2
+ )
3
+
4
+ run "rails g scaffold post title:string"
5
+
6
+ gsub_file "app/models/post.rb", "end", %(
7
+ has_magick_title
8
+
9
+ end)
10
+
11
+ gsub_file "config/routes.rb", "resources :posts", %(
12
+ resources :posts
13
+ root :to => "posts#index")
14
+
15
+ gsub_file "app/views/posts/show.html.erb", "<%= @post.title %>", %(
16
+ <%= magick_title_for @post %>)
17
+
18
+
19
+ say_status 'copying', 'sample fonts to dummy'
20
+ FileUtils.cp_r File.join(root_path, "test/fonts"), destination_path
@@ -0,0 +1 @@
1
+ rake "db:migrate", :env => "development"
@@ -0,0 +1 @@
1
+ run "rails g has_magick_title:install"
@@ -0,0 +1,28 @@
1
+ module HasMagickTitle
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+
6
+ def self.count!
7
+ @count ||= 0
8
+ (@count += 1) * 3
9
+ end
10
+
11
+ def self.next_migration_number(path)
12
+ @time ||= Time.new.utc
13
+ if ActiveRecord::Base.timestamped_migrations
14
+ (@time + self.count!).strftime("%Y%m%d%H%M%S")
15
+ else
16
+ "%.3d" % (current_migration_number(dirname) + 1)
17
+ end
18
+ end
19
+
20
+ desc "Installs required migrations for spree_essentials"
21
+ source_root File.expand_path("../../templates", __FILE__)
22
+
23
+ def copy_migrations
24
+ migration_template "add_has_magick_title.rb", "db/migrate/add_has_magick_title.rb"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ class AddHasMagickTitle < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :image_titles do |t|
4
+ t.references :imagable, :polymorphic => true
5
+ t.string :text
6
+ t.string :filename
7
+ t.integer :width
8
+ t.integer :height
9
+ t.integer :size
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :image_titles
15
+ end
16
+ end
@@ -0,0 +1,70 @@
1
+ require 'magick_title'
2
+ require 'magick_title/extension'
3
+ require 'has_magick_title/image_title'
4
+ require 'has_magick_title/view_helper'
5
+
6
+ module HasMagickTitle
7
+
8
+ module Base
9
+
10
+ def self.included(base)
11
+ base.extend(ClassMethods)
12
+ end
13
+
14
+ module ClassMethods
15
+
16
+ def has_magick_title(attribute=:title, options={})
17
+ include InstanceMethods
18
+
19
+ cattr_accessor :magick_title_options
20
+
21
+ if attribute.is_a? Hash
22
+ options = attribute
23
+ attribute = :title
24
+ end
25
+
26
+ if options.is_a? Symbol
27
+ self.magick_title_options = MagickTitle.styles[options]
28
+ elsif options.is_a? Hash
29
+ self.magick_title_options = MagickTitle.options.merge(options)
30
+ else
31
+ raise ArgumentError, "has_magick_title options must be a Symbol or Hash"
32
+ end
33
+
34
+ self.magick_title_options.merge!(:attribute => attribute)
35
+
36
+ has_one :image_title, :as => :imagable, :autosave => true, :dependent => :destroy
37
+ before_save :refresh_magick_title
38
+
39
+ end
40
+
41
+ end
42
+
43
+ module InstanceMethods
44
+
45
+ def has_magick_title?
46
+ !image_title.nil?
47
+ end
48
+ alias :has_image_title? :has_magick_title?
49
+
50
+ def magick_title_text
51
+ send magick_title_options[:attribute]
52
+ end
53
+
54
+ def refresh_magick_title(opts={})
55
+ self.image_title = build_image_title unless has_magick_title?
56
+ if opts[:force] || (image_title.new_record? || send("#{magick_title_options[:attribute]}_changed?"))
57
+ image_title.send(:delete_magick_title)
58
+ image = MagickTitle::Image.create(magick_title_text, magick_title_options)
59
+ image_title.update_attributes(image.identify.merge(:filename => image.filename))
60
+ end
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ ActiveRecord::Base.send(:include, HasMagickTitle::Base)
70
+ ActionView::Helpers.send(:include, HasMagickTitle::ViewHelper)
@@ -0,0 +1,25 @@
1
+ class ImageTitle < ActiveRecord::Base
2
+
3
+ belongs_to :imagable, :polymorphic => true
4
+
5
+ before_destroy :delete_magick_title
6
+
7
+ validate :filename, :presence => true
8
+ validate :width, :height, :size, :numericality => true
9
+
10
+ def url
11
+ File.join("/system/titles", filename)
12
+ end
13
+
14
+ def full_path
15
+ File.join(Rails.root, "public", url)
16
+ end
17
+
18
+ private
19
+
20
+ def delete_magick_title
21
+ return if new_record? || filename.nil? || !File.exists?(full_path)
22
+ FileUtils.rm(full_path)
23
+ end
24
+
25
+ end
@@ -0,0 +1,3 @@
1
+ module HasMagickTitle
2
+ VERSION = "0.1.0.rc1"
3
+ end
@@ -0,0 +1,13 @@
1
+ module HasMagickTitle
2
+ module ViewHelper
3
+
4
+ # Creates and HTML image tag with the options provided
5
+ def magick_title_for(record, options={})
6
+ opts = record.magick_title_options[:to_html].merge(options)
7
+ record.refresh_magick_title unless record.has_image_title?
8
+ return "[magick_title error]" unless record.has_image_title?
9
+ MagickTitle::Renderer.to_html(record.magick_title_text, record.image_title.url, opts).html_safe
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ MagickTitle::Options.class_eval do
2
+
3
+ # Alias the defaults for use later
4
+ alias :standard_defaults :defaults
5
+
6
+ # Overwrite defaults to include the attribute option
7
+ def defaults
8
+ standard_defaults.merge(:attribute => :title)
9
+ end
10
+
11
+ end
Binary file
@@ -0,0 +1,25 @@
1
+ # Configure Rails Envinronment
2
+ ENV["RAILS_ENV"] = "test"
3
+ require 'spork'
4
+
5
+ Spork.prefork do
6
+
7
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
8
+ require "rails/test_help"
9
+ require "shoulda"
10
+ require "sqlite3"
11
+
12
+ Rails.backtrace_cleaner.remove_silencers!
13
+
14
+ # Run any available migration if needed
15
+ ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
16
+
17
+ MagickTitle.options[:root] = Rails.root
18
+
19
+ end
20
+
21
+ Spork.each_run do
22
+
23
+ #Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| load f }
24
+
25
+ end
@@ -0,0 +1,86 @@
1
+ require_relative '../test_helper'
2
+
3
+ class HasMagickTitleTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ # nada
7
+ end
8
+
9
+ should "have has_magick_title in active record's singleton_methods" do
10
+ assert ActiveRecord::Base.singleton_methods.include?(:has_magick_title)
11
+ end
12
+
13
+ should "have a model that's called has_magick_title" do
14
+ assert Post.singleton_methods.include?(:has_magick_title)
15
+ end
16
+
17
+ context "with a magick_title model" do
18
+
19
+ setup do
20
+ @post = Post.new(:title => "A posty post")
21
+ end
22
+
23
+ should "have proper instance methods" do
24
+ [:image_title, :image_title=, :has_image_title?, :has_magick_title?, :refresh_magick_title, :magick_title_options ].each do |method|
25
+ assert @post.respond_to?(method)
26
+ end
27
+ end
28
+
29
+ should "create an image upon save" do
30
+ assert @post.valid?
31
+ assert @post.save
32
+ assert_not_nil @post.image_title
33
+ path = @post.image_title.full_path
34
+ assert File.exists?(path), "#{path} should exist"
35
+ end
36
+
37
+ context "an existing post" do
38
+
39
+ setup do
40
+ @post = Post.create(:title => "Hello McPosty")
41
+ @path = @post.image_title.full_path
42
+ end
43
+
44
+ should "do nothing if the attribute is unchanged" do
45
+ @post.save
46
+ new_path = @post.image_title.full_path
47
+ assert_equal @path, new_path
48
+ end
49
+
50
+ should "refresh if force option is given" do
51
+ path = @post.image_title.full_path
52
+ timestamp = File.new(path).mtime
53
+ # allow for a time change
54
+ sleep 1
55
+ @post.send(:refresh_magick_title, :force => true)
56
+ assert timestamp < File.new(path).mtime
57
+ end
58
+
59
+ should "not refresh if force option is absent" do
60
+ path = @post.image_title.full_path
61
+ timestamp = File.new(path).mtime
62
+ # allow for a time change
63
+ sleep 1
64
+ @post.send(:refresh_magick_title)
65
+ assert_equal timestamp, File.new(path).mtime
66
+ end
67
+
68
+ should "delete the old image when changed and create a new one" do
69
+ @post.title = "Another title!"
70
+ @post.save
71
+ assert !File.exists?(@path), "#{@path} shouldn't exist"
72
+ new_path = @post.image_title.full_path
73
+ assert_not_equal @path, new_path
74
+ assert File.exists?(new_path), "#{new_path} should exist"
75
+ end
76
+
77
+ should "delete the image with the record" do
78
+ @post.destroy
79
+ assert !File.exists?(@path)
80
+ end
81
+
82
+ end
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,26 @@
1
+ require_relative '../test_helper'
2
+
3
+ class ImageTitleTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @post = Post.create(:title => "A posty post")
7
+ @image_title = @post.image_title
8
+ end
9
+
10
+ should "have proper defaults" do
11
+ assert_not_nil @post
12
+ assert_not_nil @image_title
13
+ end
14
+
15
+ should "have proper instance methods" do
16
+ [ :url, :full_path ].each do |method|
17
+ assert @image_title.respond_to?(method)
18
+ end
19
+ end
20
+
21
+ should "have proper protected delete method" do
22
+ assert !@image_title.respond_to?(:delete_magick_title)
23
+ assert @image_title.private_methods.include?(:delete_magick_title)
24
+ end
25
+
26
+ end
@@ -0,0 +1,30 @@
1
+ require_relative '../test_helper'
2
+
3
+ class StyleTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+
7
+ MagickTitle.style :h1 do
8
+ font_size 50
9
+ line_height -10
10
+ end
11
+
12
+ Post.class_eval do
13
+ has_magick_title :title, :h1
14
+ end
15
+
16
+ @post = Post.create(:title => "A posty post")
17
+ @image_title = @post.image_title
18
+ end
19
+
20
+ should "have proper defaults" do
21
+ assert_not_nil @post
22
+ assert_not_nil @image_title
23
+ end
24
+
25
+ should "have have style options" do
26
+ assert_equal 50, @post.magick_title_options[:font_size]
27
+ assert_equal -10, @post.magick_title_options[:line_height]
28
+ end
29
+
30
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_magick_title
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: 6
5
+ version: 0.1.0.rc1
6
+ platform: ruby
7
+ authors:
8
+ - Spencer Steffen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-24 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: magick_title
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.2.0.rc1
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 3.0.0
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: sqlite3
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.3.3
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: shoulda
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - "="
56
+ - !ruby/object:Gem::Version
57
+ version: 2.11.3
58
+ type: :development
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: dummier
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - "="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.1.0
69
+ type: :development
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: spork
73
+ prerelease: false
74
+ requirement: &id006 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 0.9.0.rc7
80
+ type: :development
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: spork-testunit
84
+ prerelease: false
85
+ requirement: &id007 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 0.0.5
91
+ type: :development
92
+ version_requirements: *id007
93
+ description: Automagically creates an image of your rails model's title using magick_title.
94
+ email:
95
+ - spencer@citrusme.com
96
+ executables: []
97
+
98
+ extensions: []
99
+
100
+ extra_rdoc_files: []
101
+
102
+ files:
103
+ - .gitignore
104
+ - Gemfile
105
+ - LICENSE
106
+ - README.md
107
+ - Rakefile
108
+ - has_magick_title.gemspec
109
+ - lib/dummy_hooks/after_app_generator.rb
110
+ - lib/dummy_hooks/after_migrate.rb
111
+ - lib/dummy_hooks/before_migrate.rb
112
+ - lib/generators/has_magick_title/install_generator.rb
113
+ - lib/generators/templates/add_has_magick_title.rb
114
+ - lib/has_magick_title.rb
115
+ - lib/has_magick_title/image_title.rb
116
+ - lib/has_magick_title/version.rb
117
+ - lib/has_magick_title/view_helper.rb
118
+ - lib/magick_title/extension.rb
119
+ - test/fonts/PermanentMarker.ttf
120
+ - test/test_helper.rb
121
+ - test/unit/has_magick_title_test.rb
122
+ - test/unit/image_title_test.rb
123
+ - test/unit/style_test.rb
124
+ has_rdoc: true
125
+ homepage: https://github.com/citrus/has_magick_title
126
+ licenses: []
127
+
128
+ post_install_message:
129
+ rdoc_options: []
130
+
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: "0"
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ">"
143
+ - !ruby/object:Gem::Version
144
+ version: 1.3.1
145
+ requirements: []
146
+
147
+ rubyforge_project: has_magick_title
148
+ rubygems_version: 1.6.2
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: Automagically creates an image of your rails model's title using magick_title.
152
+ test_files:
153
+ - test/fonts/PermanentMarker.ttf
154
+ - test/test_helper.rb
155
+ - test/unit/has_magick_title_test.rb
156
+ - test/unit/image_title_test.rb
157
+ - test/unit/style_test.rb