Sha256: c64b16faa2caf5b804c33c0a77a36b9f2755b4bab3b51bfd586fa1eaede7adc2

Contents?: true

Size: 1.73 KB

Versions: 15

Compression:

Stored size: 1.73 KB

Contents

module Adminpanel
  class Section < ActiveRecord::Base
    include Adminpanel::Base

    mount_images :sectionfiles

    validates_length_of :description,
        minimum: 10,
        maximum: 10,
        allow_blank: true,
        if: :is_a_phone?,
        message: I18n.t('activerecord.errors.messages.not_phone')
    validates_presence_of :description,
        minimum: 9,
        on: :update,
        if: :has_description
    validates_presence_of :key
    validates_presence_of :name
    validates_presence_of :page

    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates_format_of :description, with: VALID_EMAIL_REGEX, if: :is_email?, allow_blank: true

    default_scope do
      order order: :asc
    end

    scope :of_page, -> (page) do
      where(page: page)
    end

    scope :with_description, -> do
      where.not(description: '')
    end

    def self.form_attributes
      [
        {'description' => {'name' => 'Descripcion', 'description' => 'label', 'label' => 'Seccion'}},
        {'name' => {'name' => 'name', 'label' => 'Seccion'}},
        {'key' => {'name' => 'key', 'label' => 'Llave'}},
        {'page' => {'name' => 'page'}},
        {'sectionfiles' => {'type' => 'adminpanel_file_field', 'show' => false}},
      ]
    end

    def self.icon
      'tasks'
    end

    def self.display_name
      I18n.t('model.Section')
    end

    def description
      if self.has_description
        return super.try(:html_safe)
      else
        return super
      end
    end

    def self.routes_options
      { path: collection_name.parameterize, except: [:new, :create, :destroy] }
    end

    protected

    def is_email?
      self.key == 'email'
    end

    def is_a_phone?
      self.key == 'phone'
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
adminpanel-3.6.1 app/models/adminpanel/section.rb
adminpanel-3.6.0 app/models/adminpanel/section.rb
adminpanel-3.5.1 app/models/adminpanel/section.rb
adminpanel-3.5.0 app/models/adminpanel/section.rb
adminpanel-3.4.7 app/models/adminpanel/section.rb
adminpanel-3.4.6 app/models/adminpanel/section.rb
adminpanel-3.4.5 app/models/adminpanel/section.rb
adminpanel-3.4.4 app/models/adminpanel/section.rb
adminpanel-3.4.3 app/models/adminpanel/section.rb
adminpanel-3.4.2 app/models/adminpanel/section.rb
adminpanel-3.4.1 app/models/adminpanel/section.rb
adminpanel-3.4.0 app/models/adminpanel/section.rb
adminpanel-3.3.4 app/models/adminpanel/section.rb
adminpanel-3.3.3 app/models/adminpanel/section.rb
adminpanel-3.3.0 app/models/adminpanel/section.rb