Giter Club home page Giter Club logo

selection_options_for's Introduction

SelectionOptionsFor

This code allows you to keep the display labels in the model when the DB holds only a 1 character flag. and when the code requires symbolic references to the value to use in algorithms

Note

Please see also the new 'state_objects' Gem StateObjects also supports behavior changes based on state and reduces conditional logic. https://github.com/mwindholtz/state_objects

Installation

Add this line to your application's Gemfile:

gem 'selection_options_for'

And then execute:

$ bundle

Or install it yourself as:

gem install selection_options_for

Usage

This code allows you to keep the display labels in the model when the DB holds only a 1 character flag. and when the code requires symbolic references to the value to use in algorithms

  • element 0 of the array passed in is always the logical symbol
  • 3-element Array is passed in, [key, DB value, label]
  • Removed: (2-element Array. This is no longer supported)
  • Any other type passed in throws an error

Limitations:

Don't use this if you will run reports directly against the DB In that case, the reports will not have access to the display labels

class Article < ActiveRecord::Base
        selection_options_for :file_type_option,
           [:pdf,    'P', 'PDF'],
           [:html,   'H', 'HTML'],
           [:msword, 'M', 'MS-Word']
           [:text,   'X', 'Textfile']
end

adds the following CLASS METHODS to Article

  • file_type_options returns a array of 2-value arrays suitable to fill a select tag The second example shows how to start the selection on a blank

    <%= select :article, :file_type_option, Article.file_type_options %> <%= select :article, :file_type_option, [['','']] + Article.file_type_options %>

    assert_equal ["MS-Word", "PDF", "HTML"], Article.file_type_option_hash.values assert_equal "['MS-Word', 'PDF', 'HTML']", Article.file_type_option_js_list

    file_type_option_symbols # returns hash of symbols

adds the following INSTANCE METHODS to Article

file_type_option_hash
file_type_option #  returns the single character value as in the db    
file_type_option_label  # returns the current values label    
file_type_option_symbol  # returns the current values symbol
  • methods ending in '?' return boolean if the value is set

  • methods ending in '!' set the value

    file_type_option_pdf?
    file_type_option_pdf!

    file_type_option_html? file_type_option_html!

    file_type_option_msword? file_type_option_msword!

example #1: Selection list

article = Article.new
article.file_type_option_pdf!
assert_equal 'P',   article.file_type_option
assert_equal :pdf,  article.file_type_symbol
assert_equal true,  article.file_type_option_pdf?
assert_equal 'PDF', article.file_type_option_label
assert_equal [["MS-Word", "M"], ["PDF", "P"], ["HTML", "H"]], 
            Article.file_type_options
assert_equal({"M"=>"MS-Word", "P"=>"PDF", "H"=>"HTML"}, 
               Article.file_type_option_hash) 
  
assert_equals({'P'=>:pdf, 'H'=>:html, 'W'=>:msword, 'T'=>:text},
                Article.file_type_option_symbols) 

By default the value first letter of the label is used as the one character value in the database field. When there are duplicate first letters you can specify a different letter to be stored in the database In the example below 'R' is stored in the database when 'Credit Card Account' is selected.

Example #2: Selection list

class Article < ActiveRecord::Base
 selection_options_for :payment_method_option,
     [:basic, 'B', 'Basic'],
     [:cash,  'C', 'Cash Account'],
     [:cc,    'R','Credit Card Account']
end

<%=  select :article, :payment_method_option, Article.payment_method_options %> 

Example #3: Radio button labels

<% Article.payment_method_option_hash.each do | key, value | %>
    <%=  radio_button :article, :payment_method_option, key %> <%= value %><br />
<% end %>

Example #4 in a java_script list

payment_method_option_js_list

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Thanks To

  • Scott Baron - for helping with the unit tests.

selection_options_for's People

Contributors

mwindholtz avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.