Sha256: d810b6cf482b9f849496e89cb2ccf53e680446bb58763ac5af4a73a17b3652c7

Contents?: true

Size: 1.42 KB

Versions: 10

Compression:

Stored size: 1.42 KB

Contents

module RailsAdmin
  module Adapters
    module Mongoid
      module Extension
        extend ActiveSupport::Concern

        included do
          class_attribute :nested_attributes_options
          self.nested_attributes_options = {}
          class << self
            def rails_admin(&block)
              RailsAdmin.config(self, &block)
            end
            alias_method_chain :accepts_nested_attributes_for, :rails_admin
          end
        end

        def rails_admin_default_object_label_method
          self.new_record? ? "new #{self.class}" : "#{self.class} ##{id}"
        end

        def safe_send(value)
          if attributes.detect { |k, _v| k.to_s == value.to_s }
            read_attribute(value)
          else
            send(value)
          end
        end

        module ClassMethods
          # Mongoid accepts_nested_attributes_for does not store options in accessible scope,
          # so we intercept the call and store it in instance variable which can be accessed from outside
          def accepts_nested_attributes_for_with_rails_admin(*args)
            options = args.extract_options!
            args.each do |arg|
              nested_attributes_options[arg.to_sym] = options.reverse_merge(allow_destroy: false, update_only: false)
            end
            args << options
            accepts_nested_attributes_for_without_rails_admin(*args)
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
rails_admin-1.0.0.rc lib/rails_admin/adapters/mongoid/extension.rb
rails_admin-0.8.1 lib/rails_admin/adapters/mongoid/extension.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/rails_admin-0.7.0/lib/rails_admin/adapters/mongoid/extension.rb
rails_admin-0.7.0 lib/rails_admin/adapters/mongoid/extension.rb
rails_admin-0.6.8 lib/rails_admin/adapters/mongoid/extension.rb
rails_admin-0.6.7 lib/rails_admin/adapters/mongoid/extension.rb
rails_admin-0.6.6 lib/rails_admin/adapters/mongoid/extension.rb
rails_admin-0.6.5 lib/rails_admin/adapters/mongoid/extension.rb
rails_admin-0.6.4 lib/rails_admin/adapters/mongoid/extension.rb
rails_admin-0.6.3 lib/rails_admin/adapters/mongoid/extension.rb