Permalink
Browse files

Adding basic usage (thought-code), not guaranteed to work

  • Loading branch information...
1 parent 0c7847a commit a2cd969324c2e26865ec367c002bbed9e4f3813d @ajn committed Oct 20, 2010
@@ -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
@@ -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
@@ -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.