Repository: plataformatec/show_for Branch: main Commit: 0c847172d160 Files: 40 Total size: 62.1 KB Directory structure: gitextract_7x2s2uzu/ ├── .github/ │ ├── code-scanning.yml │ └── workflows/ │ └── test.yml ├── .gitignore ├── CHANGELOG.md ├── Gemfile ├── MIT-LICENSE ├── README.md ├── Rakefile ├── gemfiles/ │ ├── Gemfile-rails-7-0 │ ├── Gemfile-rails-7-1 │ ├── Gemfile-rails-7-2 │ ├── Gemfile-rails-8-0 │ └── Gemfile-rails-main ├── lib/ │ ├── generators/ │ │ └── show_for/ │ │ ├── USAGE │ │ ├── install_generator.rb │ │ └── templates/ │ │ ├── en.yml │ │ ├── show.html.erb │ │ ├── show.html.haml │ │ ├── show.html.slim │ │ └── show_for.rb │ ├── show_for/ │ │ ├── association.rb │ │ ├── attribute.rb │ │ ├── builder.rb │ │ ├── content.rb │ │ ├── helper.rb │ │ ├── label.rb │ │ └── version.rb │ └── show_for.rb ├── show_for.gemspec └── test/ ├── association_test.rb ├── attribute_test.rb ├── builder_test.rb ├── content_test.rb ├── generators/ │ └── show_for_generator_test.rb ├── helper_test.rb ├── label_test.rb ├── support/ │ ├── misc_helpers.rb │ └── models.rb ├── test_helper.rb └── value_test.rb ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/code-scanning.yml ================================================ paths-ignore: - test/** ================================================ FILE: .github/workflows/test.yml ================================================ name: Test permissions: contents: read on: push: branches: - main pull_request: workflow_dispatch: jobs: test: strategy: fail-fast: false matrix: gemfile: - Gemfile - gemfiles/Gemfile-rails-main - gemfiles/Gemfile-rails-8-0 - gemfiles/Gemfile-rails-7-2 - gemfiles/Gemfile-rails-7-1 - gemfiles/Gemfile-rails-7-0 ruby: - '4.0' - '3.4' - '3.3' - '3.2' - '3.1' - '3.0' - '2.7' exclude: - gemfile: Gemfile ruby: '3.1' - gemfile: Gemfile ruby: '3.0' - gemfile: Gemfile ruby: '2.7' - gemfile: gemfiles/Gemfile-rails-main ruby: '3.2' - gemfile: gemfiles/Gemfile-rails-main ruby: '3.1' - gemfile: gemfiles/Gemfile-rails-main ruby: '3.0' - gemfile: gemfiles/Gemfile-rails-main ruby: '2.7' - gemfile: gemfiles/Gemfile-rails-8-0 ruby: '3.1' - gemfile: gemfiles/Gemfile-rails-8-0 ruby: '3.0' - gemfile: gemfiles/Gemfile-rails-8-0 ruby: '2.7' - gemfile: gemfiles/Gemfile-rails-7-2 ruby: '3.0' - gemfile: gemfiles/Gemfile-rails-7-2 ruby: '2.7' runs-on: ubuntu-latest env: # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps BUNDLE_GEMFILE: ${{ matrix.gemfile }} steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} bundler-cache: true # runs bundle install and caches installed gems automatically - run: bundle exec rake ================================================ FILE: .gitignore ================================================ .bundle/ pkg/ gemfiles/*.lock ================================================ FILE: CHANGELOG.md ================================================ ## Unreleased * Ruby 4.0 support (no changes required) ## 0.9.0 * Add support for Ruby 3.3/3.4 and Rails 7.2/8.0/8.1. (no changes required) * Drop support for Rails < 7 and Ruby < 2.7. ## 0.8.1 * Add support for Rails 7.0/7.1 and Ruby 3.1/3.2 (no changes required) * Remove test files from the gem package. ## 0.8.0 * Respect `nil` value when provided, by skipping model attribute value. * Add support for Ruby 3.0, drop support for Ruby < 2.5. * Add support for Rails 6.1, drop support for Rails < 5.2. * Move CI to GitHub Actions. ## 0.7.0 * Add support for Rails 6.0. * Drop support for Rails < 5.0 and Ruby < 2.4. ## 0.6.1 * Add support for Rails 5.2. * Fix issue causing blank class not being applied to field wrapper when the result of a Proc is empty. ## 0.6.0 * Relaxed dependencies to support Rails 5.1. ## 0.5.0 * Relaxed dependencies to support Rails 5. * Removed support for Rails `3.2` and `4.0` and Ruby `1.9.3` and `2.0.0`. ### enhancements * Do not generate label/content/wrapper/collection classes when config is set to `nil`. ## 0.4.0 ### enhancements * Change default `wrapper_tag` to generate a `div` instead of `p`. The `p` tag generates invalid HTML with collections: `p > ul`, and failed tests on Rails 4.2. If you depend on the `p` tag being generated, change your Show For config to set `wrapper_tag` to `:p`. * Remove deprecated `:method` in favor of `:using`. * Improve support to Rails 4.x associations with more duck typing instead of Array checks. * Support Rails 4.1/4.2 and Ruby 2.1/2.2. * Add `skip_blanks` configuration option to skip generating blank attributes instead of generating them with a default message. (by github.com/moktin) * Add `slim` template for the install generator. (by github.com/voanhduy1512) * Add `separator` option that overrides the global configuration (by github.com/bernardofire) ### bugfix * Do not yield default blank value to the block when using an empty association. (by github.com/aptinio) ## 0.3.0 ### bug fix * Fix blank value not being applied when using a block. (by github.com/tfwright) ## 0.3.0.rc ### enhancements * Support Rails 4. * Drop support to Rails < 3.2 and Ruby 1.8. ## 0.2.6 ### enhancements * Ruby 2.0 support. * Add Haml template. (by github.com/nashby) Closes #12. * Add `blank_html` tanslation. (by github.com/nashby) * Add `show_for_class` configuration option. (by github.com/nashby) ### bug fix * Make show_for works with namespaced models. (by github.com/fabiokr) * Add `blank_content_class` to the attributes. (by github.com/blakehilscher). Closes #24. * Don't call `association.map` if association method is nil (by github.com/nashby). Closes #40. ## 0.2.5 ### enhancements * Add a `:value` option for attribute (by github.com/ml-gt) * Add `label_class`, `content_class`, `wrapper_class` and `collection_class` configuration options (by github.com/wojtekmach) ### bug fix * Fix problem with `label => false` and `html_safe` (label => false) (by github.com/nashby) ## 0.2.4 ### enhancements * Do not add separator if label is not present (by github.com/eugenebolshakov) * Add method for output value only (by github.com/jenkek) ### bug fix * Fix empty labels to be `html_safe` (label => false) (by github.com/eugenebolshakov) ## 0.2.3 ### enhancements * added :attributes method to generate all attributes given * update generator to use the new syntax show_for:install ### bug fix * fix numeric types ## 0.2.2 ### bug fix * allow show_for to work with AR association proxies ### enhancements * improvements on html safe and output buffers ## 0.2.1 ### enhancements * added label_proc * compatibility with latest Rails ## 0.2.0 ### enhancements * Rails 3 compatibility ## 0.1.4 ### bug fix * allow show_for to work with AR association proxies ## 0.1.3 ### enhancements * allow builder to be given to show_for ### bug fix * Fix typo in yaml ## 0.1.2 ### enhancements * allow `f.attribute :nickname, in: :profile` as association shortcut ### deprecations * `:method` now becomes `:using` ## 0.1.1 ### enhancements * HAML compatibility (by github.com/grimen) * blank_content support (by github.com/grimen) ## 0.1 ### First release ================================================ FILE: Gemfile ================================================ source 'https://rubygems.org' gemspec gem 'railties', '~> 8.1.0' gem 'activemodel', '~> 8.1.0' gem 'actionpack', '~> 8.1.0' ================================================ FILE: MIT-LICENSE ================================================ Copyright (c) 2020-CURRENT Rafael França, Carlos Antonio da Silva Copyright (c) 2012-2019 Plataformatec Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # ShowFor [![Gem Version](https://badge.fury.io/rb/show_for.svg)](https://badge.fury.io/rb/show_for) ShowFor allows you to quickly show a model information with I18n features. ```erb <%= show_for @user do |u| %> <%= u.attribute :name %> <%= u.attribute :nickname, in: :profile %> <%= u.attribute :confirmed? %> <%= u.attribute :created_at, format: :short %> <%= u.attribute :last_sign_in_at, if_blank: "User did not access yet", wrapper_html: { id: "sign_in_timestamp" } %> <%= u.attribute :photo do %> <%= image_tag(@user.photo_url) %> <% end %> <%= u.association :company %> <%= u.association :tags, to_sentence: true %> <% end %> ``` ## Installation Install the gem: gem install show_for Or add ShowFor to your Gemfile and bundle it up: gem 'show_for' Run the generator: rails generate show_for:install And you are ready to go. Note: This branch aims Rails 7 and 8 support, so if you want to use it with older versions of Rails, check out the available branches. ## Usage ShowFor allows you to quickly show a model information with I18n features. ```erb <%= show_for @admin do |a| %> <%= a.attribute :name %> <%= a.attribute :login, value: :upcase %> <%= a.attribute :confirmed? %> <%= a.attribute :created_at, format: :short %> <%= a.attribute :last_sign_in_at, if_blank: "Administrator did not access yet" wrapper_html: { id: "sign_in_timestamp" } %> <%= a.attribute :photo do %> <%= image_tag(@admin.photo_url) %> <% end %> <% a.value :biography %> <% end %> ``` Will generate something like: ```html
Name
José Valim
Login
JVALIM
Confirmed?
Yes
Created at
13/12/2009 - 19h17
Last sign in at
Administrator did not access yet
Photo
Etiam porttitor eros ut diam vestibulum et blandit lectus tempor. Donec venenatis fermentum nunc ac dignissim. Pellentesque volutpat eros quis enim mollis bibendum. Ut cursus sem ac sem accumsan nec porttitor felis luctus. Sed purus nunc, auctor vitae consectetur pharetra, tristique non nisi.
``` You can also show a list of attributes, useful if you don't need to change any configuration: ```erb <%= show_for @admin do |a| %> <%= a.attributes :name, :confirmed?, :created_at %> <% end %> ``` ## Value lookup ShowFor uses the following sequence to get the attribute value: * use the output of a block argument if given * use the output of the `:value` argument if given * check if a `:"human_#{attribute}"` method is defined * retrieve the attribute directly. ## Options ShowFor handles a series of options. Those are: * __:format__ - Sent to `I18n.localize` when the attribute is a date/time object. * __:value__ - Can be used instead of block. If a `Symbol` is called as instance method. * __:if_blank__ - An object to be used if the value is blank. Not escaped as well. * __:separator__ - The piece of html that separates label and content, overriding the global configuration. In addition, all containers (`:label`, `:content` and `:wrapper`) can have their html options configured through the `:label_html`, `:content_html` and `:wrapper_html` options. Containers can have their tags configured on demand as well through `:label_tag,` `:content_tag` and `:wrapper_tag` options. ## Label ShowFor also exposes the label method. In case you want to use the default `human_attribute_name` lookup and the default wrapping: ```ruby a.label :name #=> Name a.label "Name", id: "my_name" #=> Name ``` Optionally, if you want to wrap the inner part of the label with some text (e.g. adding a semicolon), you can do so by specifying a proc for `ShowFor.label_proc` that will be called with any label text. E.g.: ```ruby ShowFor.label_proc = lambda { |l| l + ":" } ``` When taking this route, you can also skip on a per label basis by passing the `:wrap_label` option with a value of false. ## Associations ShowFor also supports associations. ```erb <%= show_for @artwork do |a| %> <%= a.association :artist %> <%= a.association :artist, using: :name_with_title %> <%= a.attribute :name_with_title, in: :artist %> <%= a.association :tags %> <%= a.association :tags, to_sentence: true %> <%= a.association :tags do @artwork.tags.map(&:name).to_sentence end %> <%= a.association :fans, collection_tag: :ol do |fan| %>
  • <%= link_to fan.name, fan %>
  • <% end %> <% end %> ``` The first is a `has_one` or `belongs_to` association, which works like an attribute to ShowFor, except it will retrieve the artist association and try to find a proper method from `ShowFor.association_methods` to be used. You can pass the option :using to tell (and not guess) which method from the association to use. :tags is a `has_and_belongs_to_many` association which will return a collection. ShowFor can handle collections by default by wrapping them in list (`