dragonfly_extensions 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,20 @@
1
+ Manifest.txt
2
+ README.rdoc
3
+ Rakefile
4
+ dragonfly_extensions.gemspec
5
+ lib/dragonfly_extensions.rb
6
+ lib/dragonfly_extensions/addressable.rb
7
+ lib/dragonfly_extensions/attachable.rb
8
+ lib/dragonfly_extensions/automatically_includable.rb
9
+ lib/dragonfly_extensions/billable.rb
10
+ lib/dragonfly_extensions/binnable.rb
11
+ lib/dragonfly_extensions/delayed_jobs.rb
12
+ lib/dragonfly_extensions/dragonfly_extensions.rb
13
+ lib/dragonfly_extensions/identifiable.rb
14
+ lib/dragonfly_extensions/legacy_connection.rb
15
+ lib/dragonfly_extensions/measurable.rb
16
+ lib/dragonfly_extensions/noteable.rb
17
+ lib/dragonfly_extensions/product_attribute.rb
18
+ lib/dragonfly_extensions/taggable_on.rb
19
+ lib/dragonfly_extensions/uniqueable.rb
20
+ Manifest
data/Manifest.txt ADDED
@@ -0,0 +1,20 @@
1
+ Manifest.txt
2
+ README.rdoc
3
+ Rakefile
4
+ dragonfly_extensions.gemspec
5
+ lib/dragonfly_extensions.rb
6
+ lib/dragonfly_extensions/addressable.rb
7
+ lib/dragonfly_extensions/attachable.rb
8
+ lib/dragonfly_extensions/automatically_includable.rb
9
+ lib/dragonfly_extensions/billable.rb
10
+ lib/dragonfly_extensions/binnable.rb
11
+ lib/dragonfly_extensions/delayed_jobs.rb
12
+ lib/dragonfly_extensions/dragonfly_extensions.rb
13
+ lib/dragonfly_extensions/identifiable.rb
14
+ lib/dragonfly_extensions/legacy_connection.rb
15
+ lib/dragonfly_extensions/measurable.rb
16
+ lib/dragonfly_extensions/noteable.rb
17
+ lib/dragonfly_extensions/product_attribute.rb
18
+ lib/dragonfly_extensions/taggable_on.rb
19
+ lib/dragonfly_extensions/uniqueable.rb
20
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1 @@
1
+ Yay for readme
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ # Rakefile
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'echoe'
5
+
6
+ Echoe.new('dragonfly_extensions', '1.0.0') do |p|
7
+ p.description = "Collection of Dragonfly Depot Extensions"
8
+ p.url = "http://github.com/hmasing/Dragonfly---Extensions"
9
+ p.author = "Hans Masing"
10
+ p.email = "hmasing@dragonflydepot.com"
11
+ p.ignore_pattern = ["tmp/*", "script/*"]
12
+ p.development_dependencies = []
13
+ end
14
+
15
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{dragonfly_extensions}
5
+ s.version = "1.0.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Hans Masing"]
9
+ s.date = %q{2010-05-10}
10
+ s.description = %q{Collection of Dragonfly Depot Extensions}
11
+ s.email = %q{hmasing@dragonflydepot.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/dragonfly_extensions.rb", "lib/dragonfly_extensions/addressable.rb", "lib/dragonfly_extensions/attachable.rb", "lib/dragonfly_extensions/automatically_includable.rb", "lib/dragonfly_extensions/billable.rb", "lib/dragonfly_extensions/binnable.rb", "lib/dragonfly_extensions/delayed_jobs.rb", "lib/dragonfly_extensions/dragonfly_extensions.rb", "lib/dragonfly_extensions/identifiable.rb", "lib/dragonfly_extensions/legacy_connection.rb", "lib/dragonfly_extensions/measurable.rb", "lib/dragonfly_extensions/noteable.rb", "lib/dragonfly_extensions/product_attribute.rb", "lib/dragonfly_extensions/taggable_on.rb", "lib/dragonfly_extensions/uniqueable.rb"]
13
+ s.files = ["Manifest.txt", "README.rdoc", "Rakefile", "dragonfly_extensions.gemspec", "lib/dragonfly_extensions.rb", "lib/dragonfly_extensions/addressable.rb", "lib/dragonfly_extensions/attachable.rb", "lib/dragonfly_extensions/automatically_includable.rb", "lib/dragonfly_extensions/billable.rb", "lib/dragonfly_extensions/binnable.rb", "lib/dragonfly_extensions/delayed_jobs.rb", "lib/dragonfly_extensions/dragonfly_extensions.rb", "lib/dragonfly_extensions/identifiable.rb", "lib/dragonfly_extensions/legacy_connection.rb", "lib/dragonfly_extensions/measurable.rb", "lib/dragonfly_extensions/noteable.rb", "lib/dragonfly_extensions/product_attribute.rb", "lib/dragonfly_extensions/taggable_on.rb", "lib/dragonfly_extensions/uniqueable.rb", "Manifest"]
14
+ s.homepage = %q{http://github.com/hmasing/Dragonfly---Extensions}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Dragonfly_extensions", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{dragonfly_extensions}
18
+ s.rubygems_version = %q{1.3.6}
19
+ s.summary = %q{Collection of Dragonfly Depot Extensions}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
@@ -0,0 +1,10 @@
1
+ module Addressable
2
+ def self.included(base)
3
+ base.has_many :addresses, :as => :addressable, :dependent => :destroy
4
+ base.has_one :primary_address, :class_name => "Address",
5
+ :foreign_key => "addressable_id",
6
+ :conditions => "addresses.is_primary is true"
7
+
8
+ base.accepts_nested_attributes_for :addresses, :allow_destroy => true
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ module Attachable
2
+ def self.included(base)
3
+ base.has_many :images, :as => :attachable, :dependent => :destroy
4
+ base.has_many :pdfs, :as => :attachable, :dependent => :destroy
5
+ base.has_many :documents, :as => :attachable, :dependent => :destroy
6
+ base.has_many :csvs, :as => :attachable, :dependent => :destroy
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ module AutomaticallyIncludable
2
+ def self.included(base)
3
+ base.before_create :assign_uuid
4
+ end
5
+
6
+ def assign_uuid
7
+ if self.respond_to? :uuid
8
+ self.uuid = UUIDTools::UUID.random_create.to_s #.gsub('-','')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ module Billable
2
+ def self.included(base)
3
+ end
4
+ end
@@ -0,0 +1,20 @@
1
+ module Binnable
2
+ def self.included(base)
3
+ base.has_many :bins, :as => :binnable
4
+ base.has_many :warehouses, :through => :bins
5
+
6
+ base.named_scope :in_warehouse, lambda { |warehouse|
7
+ {:joins => :warehouses, :conditions => [ "#{Warehouse.table_name}.id = ?", warehouse ] }
8
+ }
9
+ end
10
+
11
+ #
12
+ # Return the quantity of a binnable item.
13
+ # The default is in all warehouses
14
+ #
15
+ def qty(warehouse = nil)
16
+ warehouse.nil? ?
17
+ self.bins.inject(0) { |sum, bin| sum += bin.qty } :
18
+ self.bins.in_warehouse(warehouse).inject(0) { |sum, bin| sum += bin.qty }
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ class DelayedProductBatch
2
+ attr_accessor :batch_id
3
+ def initialize(batch_id)
4
+ self.batch_id = batch_id
5
+ end
6
+
7
+ def perform
8
+ ProductBatch.process(ProductBatch.find(@batch_id))
9
+ end
10
+ end
11
+
12
+ class DelayedInventoryBatch
13
+ attr_accessor :batch_id
14
+ def initialize(batch_id)
15
+ self.batch_id = batch_id
16
+ end
17
+
18
+ def perform
19
+ InventoryBatch.process(InventoryBatch.find(@batch_id))
20
+ end
21
+ end
22
+
23
+ class DelayedProductAmazonSync
24
+ attr_accessor :product_id
25
+ def initialize(product_id)
26
+ self.product_id = product_id
27
+ end
28
+
29
+ def perform
30
+ Product.amazon_sync(Product.find(@product_id))
31
+ end
32
+ end
@@ -0,0 +1,173 @@
1
+ #
2
+ # Monkey Patches
3
+ #
4
+ module ActiveRecord
5
+ class Base
6
+ def self.random
7
+ if (c = count) != 0
8
+ find(:first, :offset =>rand(c))
9
+ end
10
+ end
11
+
12
+ def self.all_polymorphic_types(name)
13
+ @poly_hash ||= {}.tap do |hash|
14
+ Dir.glob(File.join(RAILS_ROOT, "app", "models", "**", "*.rb")).each do |file|
15
+ klass = (File.basename(file, ".rb").camelize.constantize rescue nil)
16
+ next if klass.nil? or !klass.ancestors.include?(ActiveRecord::Base)
17
+ klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection|
18
+ (hash[reflection.options[:as]] ||= []) << klass
19
+ end
20
+ end
21
+ end
22
+ @poly_hash[name.to_sym]
23
+ end
24
+ end
25
+ end
26
+
27
+ module AASM
28
+ module ClassMethods
29
+ def aasm_states_hash
30
+ AASM::StateMachine[self].states.map { |state| state.name.to_s }
31
+ end
32
+ def aasm_events_hash
33
+ AASM::StateMachine[self].events.map { |event| event.first.to_s }
34
+ end
35
+ end
36
+ end
37
+
38
+
39
+ class Hashit
40
+ def initialize(hash)
41
+ hash.each do |k,v|
42
+ self.instance_variable_set("@#{k}", v)
43
+ self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})
44
+ self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})
45
+ end
46
+ end
47
+
48
+ def save
49
+ hash_to_return = {}
50
+ self.instance_variables.each do |var|
51
+ hash_to_return[var.gsub("@","")] = self.instance_variable_get(var)
52
+ end
53
+ return hash_to_return
54
+ end
55
+ end
56
+
57
+ class Numeric
58
+
59
+ def to_years_and_months
60
+ case
61
+ when self == 0
62
+ "0 months"
63
+ when self < 12
64
+ "#{ self} month#{ self == 1 ? '' : 's' }"
65
+ when self == 12
66
+ "1 year"
67
+ when self > 12
68
+ years = self / 12
69
+ months = self % 12
70
+ if (months > 0)
71
+ "#{years} year#{ years > 1 ? 's' : '' } and #{ months} month#{ months == 1 ? '' : 's' }"
72
+ else
73
+ "#{years} year#{ years > 1 ? 's' : '' }"
74
+ end
75
+ end
76
+ end
77
+
78
+ def roundup(nearest=10)
79
+ self % nearest == 0 ? self : self + nearest - (self % nearest)
80
+ end
81
+
82
+ def rounddown(nearest=10)
83
+ self % nearest == 0 ? self : self - (self % nearest)
84
+ end
85
+
86
+ def roundnearest(nearest=10)
87
+ up = roundup(nearest)
88
+ down = rounddown(nearest)
89
+ if (up-self) < (self-down)
90
+ return up
91
+ else
92
+ return down
93
+ end
94
+ end
95
+
96
+ def factorial
97
+ my_i = self.to_i
98
+ if my_i <= 0
99
+ result = 0
100
+ else
101
+ result = 1
102
+ my_i.downto(2) do |n|
103
+ result = result * n
104
+ end
105
+ end
106
+ result
107
+ end
108
+
109
+ def odd?
110
+ self & 1 != 0
111
+ end
112
+
113
+ def even?
114
+ self & 1 == 0
115
+ end
116
+ end
117
+
118
+ class String
119
+ # unicode_str.initial_caps => new_str
120
+ # returns a copy of a string with initial capitals
121
+ # "Jules-Édouard".initial_caps => "J.É."
122
+ def initial_caps
123
+ self.tr('-', ' ').split(' ').map { |word| word.chars.first.upcase.to_s + "." }.join
124
+ end
125
+
126
+ def capitalize_each_word
127
+ self.downcase.gsub(/^[a-z]|\s+[a-z]/) { |a| a.upcase }
128
+ end
129
+
130
+ def compress
131
+ self.gsub(' ','')
132
+ end
133
+
134
+ def truncate_words(length = 10, end_string = ' …')
135
+ words = self.split()
136
+ words[0..(length-1)].join(' ') + (words.length > length ? end_string : '')
137
+ end
138
+
139
+ def chunk_by_words(chunk_size = 8)
140
+ result = []
141
+ words = self.split
142
+ for chunk in words.in_groups_of(chunk_size)
143
+ result << chunk.join(' ')
144
+ end
145
+ result
146
+ end
147
+
148
+ def chunk_by_length(max_length = 40)
149
+ result = []
150
+ words = self.split
151
+ line = ''
152
+ for word in words
153
+ if (line.length > max_length) or (line.length + word.length > max_length)
154
+ result << line
155
+ line = ''
156
+ end
157
+ line << ' ' + word
158
+ end
159
+ result << line
160
+ result
161
+ end
162
+
163
+ def String.random_alphanumeric(length=6)
164
+ chars = ['A'..'Z','0'..'9'].map{|r|r.to_a}.flatten
165
+ ignore = ['0','1','L','A','E','I','O','U']
166
+ valid = (chars - ignore)
167
+ Array.new(length).map{valid[rand(valid.size)]}.join.downcase
168
+ end
169
+
170
+ def asset_escape
171
+ CGI::escape(self.downcase.downcase.gsub(/\s+/,'_').gsub(/[^[:alnum:]_]/, ''))
172
+ end
173
+ end
@@ -0,0 +1,38 @@
1
+ module Identifiable
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+
5
+ base.has_many :identifiers, :as => :identifiable, :dependent => :destroy
6
+ IdentifierType.uncached do
7
+ for record in IdentifierType.all(true)
8
+ base.has_many record.name.downcase.pluralize, :as => :identifiable
9
+ base.has_one record.name.downcase, :as => :identifiable, :order => "#{Identifier.table_name}.position ASC"
10
+ end
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ # Returns the first record identified by an array of values
16
+ def find_by_identifiers(*names)
17
+ record = Identifier.first(:conditions => ["#{Identifier.table_name}.name IN (?)", names])
18
+ record.respond_to?(:name) ? record.identifiable : nil
19
+ end
20
+ alias :find_by_identifier :find_by_identifiers
21
+
22
+ def identified_by(name)
23
+ self.find_by_identifier(name)
24
+ end
25
+
26
+ end
27
+
28
+ # Determine if a value is an identifier for a model
29
+ def identified_by?(name)
30
+ identifiers = self.identifiers.collect{|i| i.name}
31
+ identifiers.include?(name)
32
+ end
33
+
34
+ def not_identified_by?(name)
35
+ !identified_by?(name)
36
+ end
37
+
38
+ end
@@ -0,0 +1,10 @@
1
+ #
2
+ # Establish connections to the legacy database, using the pattern where a legacy model name
3
+ # is prefaced with 'Legacy', ie - LegacyProduct, LegacyOrder
4
+ #
5
+ module LegacyConnection
6
+ def self.included(base)
7
+ base.establish_connection(ActiveRecord::Base.configurations["legacy_#{RAILS_ENV}"])
8
+ base.table_name = base.model_name.gsub('Legacy','').downcase.pluralize
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ module Measurable
2
+ def self.included(base)
3
+ base.has_one :dimension, :as => :measurable, :dependent => :destroy
4
+ end
5
+
6
+ def dimension
7
+ if self[:dimension].nil?
8
+ self[:dimension] = Dimension.new
9
+ end
10
+ self[:dimension]
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Noteable
2
+ def self.included(base)
3
+ base.has_many :notes, :as => :noteable, :dependent => :destroy
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ module ProductAttribute
2
+ def self.included(base)
3
+ base.table_name = 'taggings'
4
+ base.belongs_to :tag
5
+ base.belongs_to :taggable, :polymorphic => true
6
+ base.belongs_to :tagger, :polymorphic => true
7
+
8
+ # base.default_scope :joins => [:tag, :taggable]#, :order => "#{Tag.table_name}.name ASC"
9
+ # base.named_scope :all, :select => "DISTINCT #{Tagging.table_name}.*", :conditions => "#{Tagging.table_name}.context = '#{base.model_name.downcase.pluralize}'"
10
+ base.named_scope :all, :conditions => "#{Tagging.table_name}.context = '#{base.model_name.downcase.pluralize}'"
11
+
12
+ end
13
+
14
+ def name
15
+ self.tag.name
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module TaggableOn
2
+ def self.included(base)
3
+ base.acts_as_taggable_on MODEL_TAGGINGS[base.model_name] if MODEL_TAGGINGS[base.model_name]
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module Uniqueable
2
+ def self.included(base)
3
+ base.before_create :generate_uuid
4
+ end
5
+
6
+ def generate_uuid
7
+ self.uuid = UUIDTools::UUID.timestamp_create.to_s
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module DragonflyExtensions
5
+ VERSION = '0.0.1'
6
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dragonfly_extensions
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Hans Masing
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-10 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Collection of Dragonfly Depot Extensions
22
+ email: hmasing@dragonflydepot.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.rdoc
29
+ - lib/dragonfly_extensions.rb
30
+ - lib/dragonfly_extensions/addressable.rb
31
+ - lib/dragonfly_extensions/attachable.rb
32
+ - lib/dragonfly_extensions/automatically_includable.rb
33
+ - lib/dragonfly_extensions/billable.rb
34
+ - lib/dragonfly_extensions/binnable.rb
35
+ - lib/dragonfly_extensions/delayed_jobs.rb
36
+ - lib/dragonfly_extensions/dragonfly_extensions.rb
37
+ - lib/dragonfly_extensions/identifiable.rb
38
+ - lib/dragonfly_extensions/legacy_connection.rb
39
+ - lib/dragonfly_extensions/measurable.rb
40
+ - lib/dragonfly_extensions/noteable.rb
41
+ - lib/dragonfly_extensions/product_attribute.rb
42
+ - lib/dragonfly_extensions/taggable_on.rb
43
+ - lib/dragonfly_extensions/uniqueable.rb
44
+ files:
45
+ - Manifest.txt
46
+ - README.rdoc
47
+ - Rakefile
48
+ - dragonfly_extensions.gemspec
49
+ - lib/dragonfly_extensions.rb
50
+ - lib/dragonfly_extensions/addressable.rb
51
+ - lib/dragonfly_extensions/attachable.rb
52
+ - lib/dragonfly_extensions/automatically_includable.rb
53
+ - lib/dragonfly_extensions/billable.rb
54
+ - lib/dragonfly_extensions/binnable.rb
55
+ - lib/dragonfly_extensions/delayed_jobs.rb
56
+ - lib/dragonfly_extensions/dragonfly_extensions.rb
57
+ - lib/dragonfly_extensions/identifiable.rb
58
+ - lib/dragonfly_extensions/legacy_connection.rb
59
+ - lib/dragonfly_extensions/measurable.rb
60
+ - lib/dragonfly_extensions/noteable.rb
61
+ - lib/dragonfly_extensions/product_attribute.rb
62
+ - lib/dragonfly_extensions/taggable_on.rb
63
+ - lib/dragonfly_extensions/uniqueable.rb
64
+ - Manifest
65
+ has_rdoc: true
66
+ homepage: http://github.com/hmasing/Dragonfly---Extensions
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options:
71
+ - --line-numbers
72
+ - --inline-source
73
+ - --title
74
+ - Dragonfly_extensions
75
+ - --main
76
+ - README.rdoc
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ segments:
91
+ - 1
92
+ - 2
93
+ version: "1.2"
94
+ requirements: []
95
+
96
+ rubyforge_project: dragonfly_extensions
97
+ rubygems_version: 1.3.6
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Collection of Dragonfly Depot Extensions
101
+ test_files: []
102
+