Sha256: dd5af2956ed01b5005dfe8f6cdbe0bf292c34f0764542e265ad8d2235f55705e

Contents?: true

Size: 1.46 KB

Versions: 4

Compression:

Stored size: 1.46 KB

Contents

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

        included do
          def self.rails_admin(&block)
            RailsAdmin::Config.model(self, &block)
          end
        end

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

        def safe_send(value)
          if self.attributes.find{ |k,v| k.to_s == value.to_s }
            self.read_attribute(value)
          else
            self.send(value)
          end
        end
      end

      module NestedAttributesExtension
        extend ActiveSupport::Concern

        included do
          attr_reader :nested_attributes_options
          alias_method_chain :accepts_nested_attributes_for, :rails_admin
        end

        # 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)
          @nested_attributes_options ||= {}
          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

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
rails_admin-0.0.3 lib/rails_admin/adapters/mongoid/extension.rb
rails_admin-0.0.2 lib/rails_admin/adapters/mongoid/extension.rb
upstream-rails_admin-1.0.2 lib/rails_admin/adapters/mongoid/extension.rb
rails_admin-0.0.1 lib/rails_admin/adapters/mongoid/extension.rb