Sha256: 526c9fb7c86ea17702cd9a75fd1a4ed2923cd33e763d2902631cdf3125a42308

Contents?: true

Size: 1.9 KB

Versions: 40

Compression:

Stored size: 1.9 KB

Contents

module ActiveScaffold
  module Bridges
    class FileColumn
      module FileColumnHelpers
        class << self
          def file_column_fields(klass)
            klass.instance_methods.grep(/_just_uploaded\?$/).collect{|m| m[0..-16].to_sym }
          end
          
          def generate_delete_helpers(klass)
            file_column_fields(klass).each { |field|
              klass.send :class_eval, <<-EOF, __FILE__, __LINE__ + 1  unless klass.methods.include?("#{field}_with_delete=")
                attr_reader :delete_#{field}
                
                def delete_#{field}=(value)
                  value = (value=="true") if String===value
                  return unless value
                  
                  # passing nil to the file column causes the file to be deleted.  Don't delete if we just uploaded a file!
                  self.#{field} = nil unless self.#{field}_just_uploaded?
                end
              EOF
            }
          end
          
          def klass_has_file_column_fields?(klass)
            true unless file_column_fields(klass).empty?
          end
        end
        
        def file_column_fields
          @file_column_fields||=FileColumnHelpers.file_column_fields(self)
        end
        
        def options_for_file_column_field(field)
          self.allocate.send("#{field}_options")
        end
        
        def field_has_image_version?(field, version="thumb")
          begin
            # the only way to get to the options of a particular field is to use the instance method
            options = options_for_file_column_field(field)
            versions = options[:magick][:versions]
            raise unless versions.stringify_keys[version]
            true
          rescue
            false
          end
        end
        
        def generate_delete_helpers
          FileColumnHelpers.generate_delete_helpers(self)
        end
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 2 rubygems

Version Path
active_scaffold-3.3.0.rc3 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.3.0.rc2 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.19 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.3.0.rc lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.17 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.16 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.15 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.14 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.13 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.12 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.11 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.10 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.9 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.8 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.7 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.6 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.5 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.4 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.3 lib/active_scaffold/bridges/file_column/file_column_helpers.rb
active_scaffold-3.2.2 lib/active_scaffold/bridges/file_column/file_column_helpers.rb