Skip to content

Commit

Permalink
Adding basic usage (thought-code), not guaranteed to work
Browse files Browse the repository at this point in the history
  • Loading branch information
ajn committed Oct 20, 2010
1 parent 0c7847a commit a2cd969
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/dragonfly/active_record_extensions/attachment.rb
@@ -0,0 +1,29 @@
module Dragonfly
module ActiveModelExtensions
class Attachment
class InvaidGeometryOrStyleNameError < StandardError; end

def style style_name_or_geometry, format=nil
scope = case style_name_or_geometry
when Symbol # style_name => :small_rectangle
style current_styles[style_name_or_geometry], format
when String # geometry => '100x80'
job.thumb style_name_or_geometry
when Array # geometry + format => ['100x80', :gif]
style *style_name_or_geometry
when Proc # Proc for style => lambda { thumb('100x80').process(:greyscale).encode(:png) }
job.instance_eval &style_name_or_geometry
else
raise InvaidGeometryOrStyleNameError
end
scope = scope.encode(format) if format
scope
end

def current_styles
parent_model.class.dragonfly_styles_for_attributes[attribute_name.to_sym] || {}
end

end
end
end
24 changes: 24 additions & 0 deletions lib/dragonfly/active_record_extensions/class_methods.rb
@@ -0,0 +1,24 @@
module Dragonfly
module ActiveModelExtensions
module ClassMethods
def register_dragonfly_styles macro_name, app
(class << self; self; end).class_eval do
# Defines e.g. 'image_styles' for any activerecord class body
define_method macro_name do |attribute, styles|
# Register the new styles
dragonfly_styles_for_attributes[attribute] = styles
end
end
app
end

def dragonfly_styles_for_attributes
@dragonfly_styles_for_attributes ||= begin
parent_class = ancestors.select{|a| a.is_a?(Class) }[1]
parent_class.respond_to?(:dragonfly_styles_for_attributes) ? parent_class.dragonfly_styles_for_attributes.dup : {}
end
end

end
end
end
9 changes: 9 additions & 0 deletions lib/dragonfly/app.rb
@@ -0,0 +1,9 @@
module Dragonfly
class App
def define_styles_macro mod, macro_name
already_extended = (class << mod; self; end).included_modules.include?(ActiveModelExtensions)
mod.extend(ActiveModelExtensions) unless already_extended
mod.register_dragonfly_styles(macro_name, self)
end
end
end

0 comments on commit a2cd969

Please sign in to comment.