Sha256: 4304abdd224bc58ef66ca9aefbf19e5f38cfc3ef242b86b0c1d85fd21064bf05

Contents?: true

Size: 1.5 KB

Versions: 10

Compression:

Stored size: 1.5 KB

Contents

require 'active_support/concern'

module Iord
  module Attriutes
    extend ActiveSupport::Concern

    included do
      helper_method :show_attrs
      helper_method :attrs
      helper_method :index_attrs
      helper_method :form_attrs
      helper_method :new_attrs
      helper_method :edit_attrs
    end

    def show_attrs
      @show_attrs ||= resource_class.attribute_names.map{ |i| i.to_sym } - %i(deleted_at)
    end

    def attrs
      @attrs ||= show_attrs - %i(_id created_at updated_at)
    end

    def index_attrs
      attrs
    end

    def form_attrs
      attrs
    end

    def new_attrs
      form_attrs
    end
    
    def edit_attrs
      form_attrs
    end

    protected
    def resource_params
      _attrs = @resource.nil? ? new_attrs : edit_attrs
      params.require(resource_name_u.to_sym).permit *construct_permit_params_array(_attrs)
    end

    def construct_permit_params_array(array)
      _attrs = []
      array.each do |a|
        if a.is_a? Hash
          if a.has_key? :fields
            _attrs << {(a[:attr].to_s + '_attributes').to_sym => construct_permit_params_array(a[:fields])}
          end
          _attrs << a[:attr]
        elsif a.is_a? Array
          key = a[1]
          if key.to_s.ends_with? '_ids'
            _attrs << {key => []}
          else
            _attrs << key
          end
        else
          _attrs << a
        end
      end
      # permit the destroy attribute
      _attrs << '_destroy'
      # permit the id
      _attrs << 'id'
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
iord-1.2.2 lib/iord/attributes.rb
iord-1.2.1 lib/iord/attributes.rb
iord-1.2.0 lib/iord/attributes.rb
iord-1.1.3 lib/iord/attributes.rb
iord-1.1.2 lib/iord/attributes.rb
iord-1.1.1 lib/iord/attributes.rb
iord-1.1.0 lib/iord/attributes.rb
iord-1.0.3 lib/iord/attributes.rb
iord-1.0.2 lib/iord/attributes.rb
iord-1.0.1 lib/iord/attributes.rb