Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding basic usage (thought-code), not guaranteed to work
- Loading branch information
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |