Sha256: 8cfff5a311d11af19b6f740a9fa4651d2e688e604128fe4746795d635bd45095

Contents?: true

Size: 1.48 KB

Versions: 15

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Decidim
  module Conferences
    module Admin
      # A form object used to create conference members from the admin dashboard.
      class PartnerForm < Form
        mimic :conference_partner

        attribute :name, String
        attribute :link, String
        attribute :partner_type, String
        attribute :weight, Integer, default: 0
        attribute :logo
        attribute :remove_logo

        validates :name, :partner_type, presence: true, if: ->(form) { form.logo.present? }
        validates :logo, file_size: { less_than_or_equal_to: ->(_record) { Decidim.maximum_avatar_size } }
        validate :link_format
        validates :weight, numericality: { greater_than_or_equal_to: 0 }
        validates :partner_type, inclusion: { in: Decidim::Conferences::Partner::TYPES }

        def link
          return if super.blank?

          return "http://" + super unless super.match?(%r{\A(http|https)://}i)

          super
        end

        def types
          Decidim::Conferences::Partner::TYPES.map do |type|
            [
              I18n.t(type, scope: "decidim.admin.models.partner.types"),
              type
            ]
          end
        end

        private

        def link_format
          return if link.blank?

          uri = URI.parse(link)
          errors.add :link, :invalid if !uri.is_a?(URI::HTTP) || uri.host.nil?
        rescue URI::InvalidURIError
          errors.add :link, :invalid
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
decidim-conferences-0.22.0 app/forms/decidim/conferences/admin/partner_form.rb
decidim-conferences-0.21.0 app/forms/decidim/conferences/admin/partner_form.rb
decidim-conferences-0.20.1 app/forms/decidim/conferences/admin/partner_form.rb
decidim-conferences-0.20.0 app/forms/decidim/conferences/admin/partner_form.rb
decidim-conferences-0.19.1 app/forms/decidim/conferences/admin/partner_form.rb
decidim-conferences-0.18.1 app/forms/decidim/conferences/admin/partner_form.rb
decidim-conferences-0.19.0 app/forms/decidim/conferences/admin/partner_form.rb
decidim-conferences-0.17.2 app/forms/decidim/conferences/admin/partner_form.rb
decidim-conferences-0.18.0 app/forms/decidim/conferences/admin/partner_form.rb
decidim-conferences-0.17.1 app/forms/decidim/conferences/admin/partner_form.rb
decidim-conferences-0.16.1 app/forms/decidim/conferences/admin/partner_form.rb
decidim-conferences-0.17.0 app/forms/decidim/conferences/admin/partner_form.rb
decidim-conferences-0.16.0 app/forms/decidim/conferences/admin/partner_form.rb
decidim-conferences-0.15.2 app/forms/decidim/conferences/admin/partner_form.rb
decidim-conferences-0.15.1 app/forms/decidim/conferences/admin/partner_form.rb