Skip to content

Commit

Permalink
Merge pull request #31 from goosys/wip/issue-30
Browse files Browse the repository at this point in the history
feat: Add enumerize gem for field selection in Administrate views
  • Loading branch information
goosys authored Jul 22, 2024
2 parents 90ceb35 + fa3f98b commit b3cb43a
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ gemspec

gem "rake", "~> 13.0"

gem "enumerize"
gem 'refile', require: 'refile/rails', github: 'vitalinfo/refile'
gem 'refile-mini_magick'

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- [x] Add context to Administrate::Page
- [x] Refine Field::Refine
- [x] Copy Field::Enumerize
- [x] RoleAwareResourceResolver
- [x] Refine Field::NestedHasMany & Field::HasMany
- [ ] Enhance AdministrateRansack
Expand Down Expand Up @@ -37,6 +38,7 @@ Add the following to your Gemfile.

```ruby
gem "sassc-rails"
gem "enumerize"
gem "refile", require: "refile/rails", github: "vitalinfo/refile", branch: "latest_ruby_rails"
gem "refile-mini_magick"

Expand Down
8 changes: 8 additions & 0 deletions app/views/fields/enumerize/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="field-unit__label">
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.select field.attribute, include_blank: false do %>
<%= options_for_select(field.select_options, field.selected) %>
<% end %>
</div>
1 change: 1 addition & 0 deletions app/views/fields/enumerize/_index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= field.to_s %>
1 change: 1 addition & 0 deletions app/views/fields/enumerize/_show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= field.to_s %>
23 changes: 23 additions & 0 deletions lib/administrate/field/enumerize.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'administrate/field/base'
require 'rails'

module Administrate
module Field
class Enumerize < Administrate::Field::Base
def to_s
data.text unless data.nil?
end

def select_options
resource.class.public_send(attribute.to_s).values.map {|w| [w.text, w]}
end

def selected
data unless data.nil?
end

class Engine < ::Rails::Engine
end
end
end
end
1 change: 1 addition & 0 deletions lib/administrate/mistybird.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative "mistybird/version"
require_relative "field/enumerize"
require_relative "field/refile"
require_relative "role_aware_resource_resolver"
require_relative "patches/administrate_datetime_default"
Expand Down

0 comments on commit b3cb43a

Please sign in to comment.