module Reactor::Persistence::ClassMethods

Public Instance Methods

sanitize_name(old_name) click to toggle source
# File lib/reactor/persistence.rb, line 489
def sanitize_name(old_name)
  if Reactor::Configuration.sanitize_obj_name
    character_map = {'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'ß' => 'ss', 'Ä' => 'Ae', 'Ö' => 'Oe', 'Ü' => 'Ue'}
    new_name = old_name.gsub(/[^-$a-zA-Z0-9]/) {|char| character_map[char] || '_'}.
      gsub(/__+/,'_').
      gsub(/^_+/,'').
      gsub(/_+$/,'')
    new_name
  else
    old_name
  end
end
subclass_from_attrs(attrs) click to toggle source

Detect the subclass from the inheritance column of attrs. If the inheritance column value is not self or a valid subclass, raises ActiveRecord::SubclassNotFound If this is a StrongParameters hash, and access to inheritance_column is not permitted, this will ignore the inheritance column and return nil

# File lib/reactor/persistence.rb, line 507
def subclass_from_attrs(attrs)
  subclass_name = attrs.with_indifferent_access[inheritance_column]

  if subclass_name.present? && subclass_name != self.name
    subclass = subclass_name.safe_constantize

    if subclass # this if has been added
      unless descendants.include?(subclass)
        raise ActiveRecord::SubclassNotFound.new("Invalid single-table inheritance type: #{subclass_name} is not a subclass of #{name}")
      end

      subclass
    end
  end
end
upload(data_or_io, extension, attributes={}) click to toggle source

Convenience method: it is equivalent to following call chain:

i = create(attributes)
i.upload(data_or_io, extension)
i.save!
i

Use it like this:

image = Image.upload(File.open('image.jpg'), 'ext', :name => 'image', :parent => '/')
# File lib/reactor/persistence.rb, line 540
def upload(data_or_io, extension, attributes={})
  # Try to guess the object name from filename, if it's missing
  if (data_or_io.respond_to?(:path) && !attributes.key?(:name))
    attributes[:name] = sanitize_name(File.basename(data_or_io.path, File.extname(data_or_io.path)))
  end

  instance = self.create!(attributes)# do |instance|
    instance.upload(data_or_io, extension)
    instance.save!
  #end
  instance
end

Protected Instance Methods

attribute_methods_overriden?() click to toggle source
# File lib/reactor/persistence.rb, line 554
def attribute_methods_overriden?
  self.name != 'RailsConnector::AbstractObj'
end