Sha256: ec93afb41c2a67ea65cff8f5d958d3200880e13e824d4e0e249a547af628053d

Contents?: true

Size: 1.15 KB

Versions: 11

Compression:

Stored size: 1.15 KB

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

  def self.none?
    self.count == 0
  end

  def self.to_ez
    s = self.name + ":\n"
    columns.each do |column|
      s <<  "  - #{column.name}: #{column.type}\n" unless column.name == 'id'
    end
    s
  end

  def self.dump_tables_to_ez
    (connection.tables - ['schema_migrations']).each do |table|
      puts table.classify.constantize.to_ez
    end
  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ez-1.3.0 lib/ez/model.rb
ez-1.2.0 lib/ez/model.rb
ez-1.1.3 lib/ez/model.rb
ez-1.1.2 lib/ez/model.rb
ez-1.1.1 lib/ez/model.rb
ez-1.1.0 lib/ez/model.rb
ez-1.0.0 lib/ez/model.rb
ez-0.9.9 lib/ez/model.rb
ez-0.9.7 lib/ez/model.rb
ez-0.9.6 lib/ez/model.rb
ez-0.9.5 lib/ez/model.rb