Sha256: 54437d0efdb67a4ee981e99ad05cd74a050c964645871f4a439afe7527a5808a

Contents?: true

Size: 808 Bytes

Versions: 4

Compression:

Stored size: 808 Bytes

Contents

class ActiveRecord::Base

  def self.magic_associations
    x = self.column_names.select { |n| n.ends_with? '_id' }

    x.each do |fk|
      assoc_name = fk[0, fk.length - 3]
      belongs_to assoc_name.to_sym
      assoc_name.classify.constantize.send(:has_many, self.name.underscore.pluralize.to_sym, dependent: :destroy)
    end
  end

  def inspect
    "#<#{self.class.name} #{attributes}>"
  end

  def self.read(args = nil)
    if args.nil?
      all
    elsif args.is_a?(Integer)
      find_by(id: args)
    elsif args.is_a?(String) && args.to_i > 0 && !args.index(' ')
      find_by(id: args)
    elsif args.is_a?(Hash) && args.keys.count == 1 && args.keys[0].to_sym == :id
      find_by(args)
    else
      where(args)
    end
  end

  def self.sample
    offset(rand(0...count)).first
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ez-0.9.1 lib/ez/model.rb
ez-0.9.0 lib/ez/model.rb
ez-0.8.7 lib/ez/model.rb
ez-0.8.6 lib/ez/model.rb