Skip to content

Commit

Permalink
Upload gem
Browse files Browse the repository at this point in the history
  • Loading branch information
balbinas committed Jul 5, 2016
0 parents commit 79c7871
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Administrate::Field::Enum

A plugin to show enum attributes 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.

## 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` |

[Rubygems naming guidelines]: http://guides.rubygems.org/name-your-gem/

[Administrate]: https://github.com/thoughtbot/administrate
21 changes: 21 additions & 0 deletions administrate-field-enum.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
$:.push File.expand_path('../lib', __FILE__)

require 'administrate/field/enum'

Gem::Specification.new do |gem|
gem.name = 'administrate-field-enum'
gem.version = Administrate::Field::Enum::VERSION
gem.authors = ['Balbina Santana']
gem.email = ['[email protected]']
gem.homepage = 'https://github.com/DisruptiveAngels/administrate_field_enum'
gem.summary = 'Enum field plugin for Administrate'
gem.description = gem.summary
gem.license = 'MIT'

gem.require_paths = ['lib']
gem.files = `git ls-files`.split('\n')
gem.test_files = `git ls-files -- {test,spec,features}/*`.split('\n')

gem.add_dependency 'administrate', '>= 0.2.0.rc1', '< 0.3.0'
gem.add_dependency 'rails', '~> 4.2', '< 5.1'
end
28 changes: 28 additions & 0 deletions app/views/fields/enum/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<%#
# Enum Form Partial
This partial renders an input element for enum attributes.
By default, the input is a select field for the enum attributes.
## Local variables:
- `f`:
A Rails form generator, used to help create the appropriate input fields.
- `field`:
An instance of [Administrate::Field::Enum][1].
A wrapper around the enum attributes pulled from the model.
%>

<div class="field-unit__label">
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.select(
field.attribute,
options_for_select(
f.object.class.public_send(field.attribute.to_s.pluralize)
.map { |k, v| [k.humanize, k] },
f.object.public_send(field.attribute.to_s)),
include_blank: false) %>
</div>
17 changes: 17 additions & 0 deletions app/views/fields/enum/_index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%#
# Enum Index Partial
This partial renders an enum attribute
to be displayed on a resource's index page.
By default, the attribute is rendered as a text tag.
## Local variables:
- `field`:
An instance of [Administrate::Field::Enum][1].
A wrapper around the enum attributes pulled from the model.
%>

<%= field.to_s %>
17 changes: 17 additions & 0 deletions app/views/fields/enum/_show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%#
# Enum Show Partial
This partial renders an enum attribute,
to be displayed on a resource's show page.
By default, the attribute is rendered as a text tag.
## Local variables:
- `field`:
An instance of [Administrate::Field::Enum][1].
A wrapper around the enum attributes pulled from the model.
%>

<%= field.to_s %>
13 changes: 13 additions & 0 deletions lib/administrate/field/enum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'administrate/field/base'
require 'rails'

module Administrate
module Field
class Enum < Administrate::Field::Base
VERSION = '0.0.1'

class Engine < ::Rails::Engine
end
end
end
end
14 changes: 14 additions & 0 deletions spec/lib/administrate/field/enum_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'administrate/field/enum'

describe Administrate::Field::Enum do
describe '#to_partial_path' do
it 'returns a partial based on the page being rendered' do
page = :show
field = Administrate::Field::Enum.new(:status, 'status', page)

path = field.to_partial_path

expect(path).to eq('/fields/enum/#{page}')
end
end
end

0 comments on commit 79c7871

Please sign in to comment.