Sha256: 04a609ce3cbec3c83dc52ee6f445f1ecfdbc80e1f4f9e5ddb46520cbb799d6cd

Contents?: true

Size: 1.86 KB

Versions: 3

Compression:

Stored size: 1.86 KB

Contents

module Lipsiadmin
  module DataBase
    # This class create a *fake* table that can be usefull if you need for example
    # perform validations. Take a look to this test case:
    #
    #   Examples:
    #
    #     class Contact < Lipsiadmin::DataBase::WithoutTable
    #       column :name, :string
    #       column :company, :string
    #       column :telephone, :string
    #       column :email, :string
    #       column :message, :text
    #
    #       validates_presence_of :name, :message
    #       validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
    #     end
    #
    # Now we need to validate a contact, and if the validations is okey send an email or if not raise errors
    #
    #   @contact = Contact.new(params[:contact])
    #   if @contact.valid?
    #     Notifier.deliver_support_request(@contact)
    #   else
    #     flash[:notice] = "There are some problems"
    #     render :action => :support_request
    #   end
    #
    class WithoutTable < ActiveRecord::Base
      self.abstract_class = true

      def create_or_update#:nodoc:
        errors.empty?
      end

      class << self
        # Returns columns for this *fake* table
        def columns()
          @columns ||= []
        end

        # Define columns for a this *fake* table
        def column(name, sql_type = nil, default = nil, null = true)
          columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
          reset_column_information
        end

        # Resets all the cached information about columns, which will cause them to be reloaded on the next request.
        def reset_column_information
          generated_methods.each { |name| undef_method(name) }
          @column_names = @columns_hash = @content_columns = @dynamic_methods_hash = @generated_methods = nil
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lipsiadmin-5.1.9 lib/data_base/without_table.rb
lipsiadmin-5.1.8 lib/data_base/without_table.rb
lipsiadmin-5.1.7 lib/data_base/without_table.rb