GithubHelp home page GithubHelp logo

mark100net / administrate-field-enum Goto Github PK

View Code? Open in Web Editor NEW

This project forked from guillaumemaka/administrate-field-enum

0.0 2.0 0.0 18 KB

A plugin to show enum in Administrate.

License: Other

Ruby 90.39% HTML 9.61%

administrate-field-enum's Introduction

CircleCI

Administrate::Field::Enum

A plugin to show enum in Administrate.

This repository is the first field plugin extracted out of Administrate. Although its structure may change, it's designed to act as a template for other Administrate field plugins.

Installation

gem 'administrate-field-enum', git: "https://github.com/guillaumemaka/administrate-field-enum.git", branch: "master"

Usage

In your dashboard ATTRIBUTES_TYPE

ATTRIBUTE_TYPES = {
  ...
  "status": Field:Enum
  ...
}

By default the plugin render radio buttons if the enum contains less than 4 enumerations, otherwise it render a select input box in forms and the text representation (titleized) for show and index page.

Custom collection method

By default the plugin retrieve all the enum values by calling the mapping class method pluralize attribute on your model object:

class Post < ActiveRecord::Base
  enum status: { submitted: 1, approved: 2, published: 3 }
end

In this example if we declare an attribute like this:

class PostDashboard
  ATTRIBUTE_TYPES = {
    "status": Field:Enum
  }
end

the plugin will call Post.statuses, that will return a Hash. The Hash keys will be use as the text label (titleized) and as input value.

So if for any circumstances you need to return a custom collection (usually you don't) you can provide a :collection_method option to the field, this method will be called on your model instead.

Your custom collection method should return :

  • A string/integer key/value Hash object
{ submitted: 1, approved: 2, published: 3 }
  • An Array where each element is an Array with the first element correspond to the text label and the second the corresponding value
[
    ['Submitted', 1],
    ['Approved', 2],
    ['Rejected', 3]
]
  • An Array of Hash object :
[
    { label: "Submitted", value: 1 },
    { label: "Approved", value: 2 },
    { label: "Rejected", value: 3 },
]

In the last case you must provide a label_key and value_key options to the options field :

class PostDashboard
  ATTRIBUTE_TYPES = {
    "status": Field:Enum.with_options(
      collection_method: :a_method,
      label_key: :label,
      value_key: :value)
  }
end

FAQs

Q: How should I name my gem?

A: Administrate field gems must be named according to the Rubygems naming guidelines.

Essentially, name your gem after the field class that it defines. If there's a namespace in the class name, that gets translated to a dash (-) in the gem name. If the class name is CamelCased, that translates to an underscore (_) in the gem name.

Since all administrate field gems are under the namespace Administrate::Field, every field gem name should start with the prefix administrate-field-.

Here are some examples (these don't correspond to actual gems):

Gem Name Field Name
administrate-field-enum Administrate::Field::Enum
administrate-field-file_upload Administrate::Field::FileUpload
administrate-field-geocoding-region Administrate::Field::Geocoding::Region
administrate-field-geocoding-geo_json Administrate::Field::Geocoding::GeoJson

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.