├── Gemfile ├── lib └── administrate │ └── field │ └── enum.rb ├── app └── views │ └── fields │ └── enum │ ├── _show.html.erb │ ├── _index.html.erb │ └── _form.html.erb ├── administrate-field-enum.gemspec ├── README.md ├── LICENSE.md └── spec └── lib └── administrate └── field └── enum_spec.rb /Gemfile: -------------------------------------------------------------------------------- 1 | source 'http://rubygems.org' 2 | 3 | gemspec 4 | 5 | group :development, :test do 6 | gem 'rspec-rails', '~> 3.5.0' 7 | end 8 | -------------------------------------------------------------------------------- /lib/administrate/field/enum.rb: -------------------------------------------------------------------------------- 1 | require 'administrate/field/base' 2 | require 'rails' 3 | 4 | module Administrate 5 | module Field 6 | class Enum < Administrate::Field::Base 7 | def to_s 8 | data.humanize unless data.nil? 9 | end 10 | 11 | def html_options 12 | options[:html] || {} 13 | end 14 | 15 | class Engine < ::Rails::Engine 16 | end 17 | end 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /app/views/fields/enum/_show.html.erb: -------------------------------------------------------------------------------- 1 | <%# 2 | # Enum Show Partial 3 | 4 | This partial renders an enum attribute, 5 | to be displayed on a resource's show page. 6 | 7 | By default, the attribute is rendered as a text tag. 8 | 9 | ## Local variables: 10 | 11 | - `field`: 12 | An instance of [Administrate::Field::Enum][1]. 13 | A wrapper around the enum attributes pulled from the model. 14 | 15 | %> 16 | 17 | <%= I18n.t("activerecord.attributes.#{field.resource.class.name.underscore}.#{field.attribute.to_s.pluralize}.#{field.data}", default: field.to_s) %> 18 | -------------------------------------------------------------------------------- /app/views/fields/enum/_index.html.erb: -------------------------------------------------------------------------------- 1 | <%# 2 | # Enum Index Partial 3 | 4 | This partial renders an enum attribute 5 | to be displayed on a resource's index page. 6 | 7 | By default, the attribute is rendered as a text tag. 8 | 9 | ## Local variables: 10 | 11 | - `field`: 12 | An instance of [Administrate::Field::Enum][1]. 13 | A wrapper around the enum attributes pulled from the model. 14 | 15 | %> 16 | 17 | <%= I18n.t("activerecord.attributes.#{field.resource.class.name.underscore}.#{field.attribute.to_s.pluralize}.#{field.data}", default: field.to_s) %> 18 | -------------------------------------------------------------------------------- /administrate-field-enum.gemspec: -------------------------------------------------------------------------------- 1 | $:.push File.expand_path('../lib', __FILE__) 2 | 3 | Gem::Specification.new do |s| 4 | s.name = 'administrate-field-enum' 5 | s.version = '0.0.8' 6 | s.authors = ['Balbina Santana', 'Adrian Rangel'] 7 | s.email = ['adrian@disruptiveangels.com'] 8 | s.homepage = 'https://github.com/DisruptiveAngels/administrate-field-enum' 9 | s.summary = 'Enum field plugin for Administrate' 10 | s.description = s.summary 11 | s.license = 'MIT' 12 | 13 | s.require_paths = ['lib'] 14 | s.files = `git ls-files`.split("\n") 15 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 16 | 17 | s.add_dependency 'administrate', '~> 0.12' 18 | end 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## AdministrateFieldEnum 2 | [![Gem](https://img.shields.io/gem/v/administrate-field-enum.svg)]() 3 | 4 | A plugin to show enum attributes in [Administrate]. 5 | 6 | ## Installation 7 | 8 | Add this line to your application's Gemfile: 9 | 10 | ```ruby 11 | gem 'administrate-field-enum' 12 | ``` 13 | 14 | And then execute: 15 | ``` 16 | $ bundle install 17 | ``` 18 | 19 | ## Usage 20 | 21 | In your Dashboard `ATTRIBUTE_TYPES` use the field type `Field::Enum`. i.e. 22 | ```ruby 23 | ATTRIBUTE_TYPES = { 24 | category: Field::Enum 25 | } 26 | ``` 27 | 28 | ## Contributing 29 | 30 | Bug reports and pull requests are welcome on GitHub at https://github.com/DisruptiveAngels/administrate-field-enum. 31 | 32 | ## License 33 | 34 | [MIT License](https://github.com/DisruptiveAngels/administrate-field-enum/blob/master/LICENSE.md) 35 | 36 | -------------------------------------------------------------------------------- /app/views/fields/enum/_form.html.erb: -------------------------------------------------------------------------------- 1 | <%# 2 | # Enum Form Partial 3 | 4 | This partial renders an input element for enum attributes. 5 | By default, the input is a select field for the enum attributes. 6 | 7 | ## Local variables: 8 | 9 | - `f`: 10 | A Rails form generator, used to help create the appropriate input fields. 11 | - `field`: 12 | An instance of [Administrate::Field::Enum][1]. 13 | A wrapper around the enum attributes pulled from the model. 14 | 15 | %> 16 | 17 |
18 | <%= f.label field.attribute %> 19 |
20 |
21 | <%= f.select( 22 | field.attribute, 23 | options_for_select( 24 | f.object.class.public_send(field.attribute.to_s.pluralize).map do |k, v| 25 | [I18n.t("activerecord.attributes.#{f.object.class.name.underscore}.#{field.attribute.to_s.pluralize}.#{k}", default: k.humanize), k] 26 | end, 27 | f.object.public_send(field.attribute.to_s) 28 | ), 29 | { include_blank: !f.object.class.validators_on(field.attribute.to_s).map(&:class).include?(ActiveRecord::Validations::PresenceValidator) }, 30 | field.html_options) %> 31 |
32 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 DisruptiveAngels 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /spec/lib/administrate/field/enum_spec.rb: -------------------------------------------------------------------------------- 1 | require 'administrate/field/enum' 2 | 3 | describe Administrate::Field::Enum do 4 | describe '#to_partial_path' do 5 | it 'returns a partial based on the page being rendered' do 6 | page = :show 7 | field = Administrate::Field::Enum.new(:status, 'status', page) 8 | 9 | path = field.to_partial_path 10 | 11 | expect(path).to eq("/fields/enum/#{page}") 12 | end 13 | end 14 | 15 | describe '#html_options' do 16 | it 'returns a hash of :html options' do 17 | page = :show 18 | field = Administrate::Field::Enum.with_options(html: { disabled: true, hidden: true, class: 'test' }) 19 | .new(:status, 'status', page) 20 | 21 | expect(field.html_options[:disabled]).to be_truthy 22 | expect(field.html_options[:hidden]).to be_truthy 23 | expect(field.html_options[:class]).to eq('test') 24 | end 25 | 26 | it 'is empty by default' do 27 | page = :show 28 | field = Administrate::Field::Enum.new(:status, 'status', page) 29 | 30 | expect(field.html_options[:disabled]).to be_falsy 31 | expect(field.html_options[:hidden]).to be_falsy 32 | expect(field.html_options[:class]).to be_nil 33 | end 34 | end 35 | end 36 | --------------------------------------------------------------------------------