Sha256: 15557f81969ff32913b322acb10a92f28efc1d7faa381314aed20b3a1c69adc0

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

module RailsConnector

  # This is the abstract class from which all CMS models derive.
  # The database connection is set to "infopark_rails_connector_${RAILS_ENV}" and the table prefix is
  # determined from the instance_name.
  #
  # [instance_name] the name of the CMS instance
  class CmsBaseModel < ActiveRecord::Base
    @@instance_name = 'default'
    cattr_accessor :instance_name
    self.abstract_class = true

    def self.configure_database(db_connection_spec)
      establish_connection(db_connection_spec)
      Blob.initialize_blob_streaming_for(connection.adapter_name)
    end

    # Returns :table_name_with_underscore. All CMS models use this kind of primary key naming.
    def self.primary_key_prefix_type
      :table_name_with_underscore
    end

    # CmsBaseModel and all of its descendants are model classes that can access CMS contents.
    def self.cms_model?
      true
    end

    # FIXME: Find a more stable solution for this
    #
    # Since ActiveRecord 4.2 finding Objs by name is no longer consistent when
    # switching between edited and released content at runtime. As long as we
    # find a better solution this mitigates the behaviour.
    def self.find_by(arg, *args)
      where(arg, *args).take
    end

    def readonly?
      true
    end

    class << self
      # The prefix of the table name is determined by using the instance name followed by an underscore.
      # Set the instance_name in your environment to the CMS instance name (defaults to 'default').
      #
      #   CmsBaseModel.instance_name = 'default'
      #
      # ActiveRecord::Base::primary_key_prefix_type has no effect here.
      def table_name_prefix
        @@instance_name.blank? ? '' : (@@instance_name + '_')
      end
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
infopark_fiona_connector-7.0.1.5.2.3.rc4 lib/rails_connector/cms_base_model.rb
infopark_fiona_connector-7.0.1 lib/rails_connector/cms_base_model.rb
infopark_fiona_connector-7.0.1.beta2 lib/rails_connector/cms_base_model.rb