Sha256: 0901fa77d3ca00b94d348e8cb6e18174666a63472eddc5a33d9df30c8559ebfd

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

require 'carrierwave'
require 'carrierwave/orm/activerecord'
module Adminpanel
  class Section < ActiveRecord::Base
    include Adminpanel::Base

    mount_images :sectionfiles

    validates_length_of :description,
        minimum: 10,
        maximum: 10,
        on: :update,
        if: :is_a_phone?,
        message: I18n.t('activerecord.errors.messages.not_phone')
    validates_presence_of :description,
        minimum: 9,
        on: :update,
        if: :has_description
    validates :description,
        numericality: { only_integer: true },
        on: :update,
        if: :is_a_phone?
    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?

    default_scope do
      order("page 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'}},
      ]
    end

    def self.icon
    	'tasks'
    end

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

    def description
      if self.has_description && !self.attributes['description'].nil?
        return self.attributes['description'].html_safe
      else
        return self.attributes['description']
      end
    end

    protected
    def has_description?
      !self.has_description.nil? || self.has_description
    end

    def is_email?
      key == 'email' && description != ''
    end

    def is_a_phone?
      key == 'phone' && description != ''
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
adminpanel-2.3.1 app/models/adminpanel/section.rb
adminpanel-2.3.0 app/models/adminpanel/section.rb