Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A administrate
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 96
    • Issues 96
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 32
    • Merge requests 32
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • thoughtbot, inc.
  • administrate
  • Merge requests
  • !1220

Make Field::Select more flexible

  • Review changes

  • Download
  • Email patches
  • Plain diff
Open Yamamoto-Work requested to merge github/fork/jumjamjohn/master into main Sep 25, 2018
  • Overview 38
  • Commits 7
  • Pipelines 1
  • Changes 5
  • This PR adds more flexibility to Field::Select so we can set an array of arrays and a hash to collection option. This behavior would be more intuitive since it follows Rails form helper convention.
  • This also solves the absence of Enum field referred to #322 and #533 (closed) so we don't have to create a Enum field as a custom or built-in field anymore.
  • Added select_spec.rb so we can test Select field

By default, it displays label data(e.g. "Pending") instead of raw persisted data(e.g. 1) on show/index page. Search feature would be an issue because users would want to search by label data but the system searches only by persisted data for the moment. However, that issue should be solved by another PR, not this one. Thank you for this awesome gem 😃

Basic usage:

status: Field::Select.with_options(collection: [ ['Pending',1], ['Approved',2], ['Rejected',3] ])

=>

<select name="post[status]" id="post_status">
  <option selected="selected" value="1">Pending</option>
  <option value="2">Approved</option>
  <option value="3">Rejected</option>
</select>

For Enum:

status: Field::Select.with_options(collection: Post.localized_statuses.invert)
class Post < ApplicationRecord
  enum status: { submitted: 0, approved: 1, rejected: 2 }
  def self.localized_statuses
    I18n.t 'activerecord.attributes.post/status'
  end
  # or
  # def self.localized_statuses
  #   Post.statuses.keys.map {|k| [k, Post.human_attribute_name("post.status.#{k}")] }
  # end
end
activerecord:
  attributes:
    post/status:
      submitted: "承認待ち"
      approved: "承認済み"
      rejected: "取り消し"
Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/jumjamjohn/master