e9_attributes 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/app/models/menu_option.rb +7 -10
 - data/lib/e9_attributes/model.rb +27 -7
 - data/lib/e9_attributes/version.rb +1 -1
 - metadata +12 -4
 
    
        data/app/models/menu_option.rb
    CHANGED
    
    | 
         @@ -1,24 +1,21 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # A simple class to manage menu options, usable by other classes to build their menus.
         
     | 
| 
       2 
2 
     | 
    
         
             
            #
         
     | 
| 
       3 
3 
     | 
    
         
             
            class MenuOption < ActiveRecord::Base
         
     | 
| 
       4 
     | 
    
         
            -
               
     | 
| 
      
 4 
     | 
    
         
            +
              cattr_writer :keys
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
     | 
    
         
            -
              def self. 
     | 
| 
       7 
     | 
    
         
            -
                 
     | 
| 
       8 
     | 
    
         
            -
             
     | 
| 
       9 
     | 
    
         
            -
                keys.flatten!
         
     | 
| 
       10 
     | 
    
         
            -
                keys.map!(&:to_s)
         
     | 
| 
       11 
     | 
    
         
            -
             
     | 
| 
       12 
     | 
    
         
            -
                self.keys = keys
         
     | 
| 
       13 
     | 
    
         
            -
                validates :key, :presence  => true, :inclusion => { :in => keys, :allow_blank => true }
         
     | 
| 
      
 6 
     | 
    
         
            +
              def self.keys
         
     | 
| 
      
 7 
     | 
    
         
            +
                @@keys || []
         
     | 
| 
       14 
8 
     | 
    
         
             
              end
         
     | 
| 
       15 
9 
     | 
    
         | 
| 
       16 
10 
     | 
    
         
             
              validates :value, :presence  => true
         
     | 
| 
      
 11 
     | 
    
         
            +
              validate { errors.add(:key, :inclusion) if key.present? && !keys.include?(key) }
         
     | 
| 
       17 
12 
     | 
    
         | 
| 
       18 
13 
     | 
    
         
             
              acts_as_list :scope => 'menu_options.key = \"#{key}\"'
         
     | 
| 
       19 
14 
     | 
    
         | 
| 
       20 
15 
     | 
    
         
             
              scope :options_for, lambda {|key| where(:key => key).order('menu_options.position ASC') }
         
     | 
| 
       21 
16 
     | 
    
         | 
| 
      
 17 
     | 
    
         
            +
              delegate :keys, :to => 'self.class'
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
       22 
19 
     | 
    
         
             
              ##
         
     | 
| 
       23 
20 
     | 
    
         
             
              # A direct SQL selection of values for a given key
         
     | 
| 
       24 
21 
     | 
    
         
             
              #
         
     | 
| 
         @@ -29,7 +26,7 @@ class MenuOption < ActiveRecord::Base 
     | 
|
| 
       29 
26 
     | 
    
         
             
              # [key (String)] The key for the assocated menu options.
         
     | 
| 
       30 
27 
     | 
    
         
             
              #
         
     | 
| 
       31 
28 
     | 
    
         
             
              def self.fetch_values(key)
         
     | 
| 
       32 
     | 
    
         
            -
                connection.send(:select_values, options_for(key).order(:position). 
     | 
| 
      
 29 
     | 
    
         
            +
                connection.send(:select_values, options_for(key).select('value').order(:position).to_sql, 'Menu Option Select')
         
     | 
| 
       33 
30 
     | 
    
         
             
              end
         
     | 
| 
       34 
31 
     | 
    
         | 
| 
       35 
32 
     | 
    
         
             
              def to_s
         
     | 
    
        data/lib/e9_attributes/model.rb
    CHANGED
    
    | 
         @@ -2,20 +2,40 @@ module E9Attributes::Model 
     | 
|
| 
       2 
2 
     | 
    
         
             
              extend ActiveSupport::Concern
         
     | 
| 
       3 
3 
     | 
    
         | 
| 
       4 
4 
     | 
    
         
             
              module ClassMethods
         
     | 
| 
      
 5 
     | 
    
         
            +
                #
         
     | 
| 
      
 6 
     | 
    
         
            +
                # By default, it reformats the attributes passed into association
         
     | 
| 
      
 7 
     | 
    
         
            +
                # names and defines them.  To add a record attribute without defining
         
     | 
| 
      
 8 
     | 
    
         
            +
                # an association name, pass the association names literally and specify
         
     | 
| 
      
 9 
     | 
    
         
            +
                # the option :skip_name_format. This will cause the method to skip adding
         
     | 
| 
      
 10 
     | 
    
         
            +
                # associations for any attribute not ending in "_attributes", which you
         
     | 
| 
      
 11 
     | 
    
         
            +
                # must add manually, e.g.
         
     | 
| 
      
 12 
     | 
    
         
            +
                #
         
     | 
| 
      
 13 
     | 
    
         
            +
                #     has_many :users
         
     | 
| 
      
 14 
     | 
    
         
            +
                #     accepts_nested_attributes_for :users, :allow_destroy => true
         
     | 
| 
      
 15 
     | 
    
         
            +
                #
         
     | 
| 
       5 
16 
     | 
    
         
             
                def has_record_attributes(*attributes)
         
     | 
| 
       6 
17 
     | 
    
         
             
                  options = attributes.extract_options!
         
     | 
| 
      
 18 
     | 
    
         
            +
                  options.symbolize_keys!
         
     | 
| 
       7 
19 
     | 
    
         | 
| 
       8 
20 
     | 
    
         
             
                  class_inheritable_accessor :record_attributes
         
     | 
| 
       9 
     | 
    
         
            -
             
     | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
     | 
    
         
            -
             
     | 
| 
       12 
     | 
    
         
            -
             
     | 
| 
       13 
     | 
    
         
            -
             
     | 
| 
       14 
     | 
    
         
            -
             
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
                  attributes.flatten!
         
     | 
| 
      
 23 
     | 
    
         
            +
                  attributes.map!(&:to_s)
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                  unless options[:skip_name_format]
         
     | 
| 
      
 26 
     | 
    
         
            +
                    attributes.map! do |a|
         
     | 
| 
      
 27 
     | 
    
         
            +
                      a = a.classify
         
     | 
| 
      
 28 
     | 
    
         
            +
                      a = a =~ /Attribute$/ ? a : "#{a}Attribute"
         
     | 
| 
      
 29 
     | 
    
         
            +
                      a.constantize rescue next
         
     | 
| 
      
 30 
     | 
    
         
            +
                      a.underscore.pluralize
         
     | 
| 
      
 31 
     | 
    
         
            +
                    end.compact
         
     | 
| 
      
 32 
     | 
    
         
            +
                  end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                  self.record_attributes = attributes
         
     | 
| 
       15 
35 
     | 
    
         | 
| 
       16 
36 
     | 
    
         
             
                  has_many :record_attributes, :as => :record
         
     | 
| 
       17 
37 
     | 
    
         | 
| 
       18 
     | 
    
         
            -
                  self.record_attributes.each do |association_name|
         
     | 
| 
      
 38 
     | 
    
         
            +
                  self.record_attributes.select {|r| r =~ /attributes$/ }.each do |association_name|
         
     | 
| 
       19 
39 
     | 
    
         
             
                    has_many association_name.to_sym, :class_name => association_name.classify, :as => :record
         
     | 
| 
       20 
40 
     | 
    
         
             
                    accepts_nested_attributes_for association_name.to_sym, :allow_destroy => true, :reject_if => :reject_record_attribute?
         
     | 
| 
       21 
41 
     | 
    
         
             
                  end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,8 +1,12 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification 
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: e9_attributes
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version 
         
     | 
| 
       4 
     | 
    
         
            -
              prerelease: 
         
     | 
| 
       5 
     | 
    
         
            -
               
     | 
| 
      
 4 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 5 
     | 
    
         
            +
              segments: 
         
     | 
| 
      
 6 
     | 
    
         
            +
              - 0
         
     | 
| 
      
 7 
     | 
    
         
            +
              - 0
         
     | 
| 
      
 8 
     | 
    
         
            +
              - 3
         
     | 
| 
      
 9 
     | 
    
         
            +
              version: 0.0.3
         
     | 
| 
       6 
10 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       7 
11 
     | 
    
         
             
            authors: 
         
     | 
| 
       8 
12 
     | 
    
         
             
            - Travis Cox
         
     | 
| 
         @@ -10,7 +14,7 @@ autorequire: 
     | 
|
| 
       10 
14 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       11 
15 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       12 
16 
     | 
    
         | 
| 
       13 
     | 
    
         
            -
            date: 2011- 
     | 
| 
      
 17 
     | 
    
         
            +
            date: 2011-09-07 00:00:00 -04:00
         
     | 
| 
       14 
18 
     | 
    
         
             
            default_executable: 
         
     | 
| 
       15 
19 
     | 
    
         
             
            dependencies: []
         
     | 
| 
       16 
20 
     | 
    
         | 
| 
         @@ -72,17 +76,21 @@ required_ruby_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       72 
76 
     | 
    
         
             
              requirements: 
         
     | 
| 
       73 
77 
     | 
    
         
             
              - - ">="
         
     | 
| 
       74 
78 
     | 
    
         
             
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 79 
     | 
    
         
            +
                  segments: 
         
     | 
| 
      
 80 
     | 
    
         
            +
                  - 0
         
     | 
| 
       75 
81 
     | 
    
         
             
                  version: "0"
         
     | 
| 
       76 
82 
     | 
    
         
             
            required_rubygems_version: !ruby/object:Gem::Requirement 
         
     | 
| 
       77 
83 
     | 
    
         
             
              none: false
         
     | 
| 
       78 
84 
     | 
    
         
             
              requirements: 
         
     | 
| 
       79 
85 
     | 
    
         
             
              - - ">="
         
     | 
| 
       80 
86 
     | 
    
         
             
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 87 
     | 
    
         
            +
                  segments: 
         
     | 
| 
      
 88 
     | 
    
         
            +
                  - 0
         
     | 
| 
       81 
89 
     | 
    
         
             
                  version: "0"
         
     | 
| 
       82 
90 
     | 
    
         
             
            requirements: []
         
     | 
| 
       83 
91 
     | 
    
         | 
| 
       84 
92 
     | 
    
         
             
            rubyforge_project: e9_attributes
         
     | 
| 
       85 
     | 
    
         
            -
            rubygems_version: 1. 
     | 
| 
      
 93 
     | 
    
         
            +
            rubygems_version: 1.3.7
         
     | 
| 
       86 
94 
     | 
    
         
             
            signing_key: 
         
     | 
| 
       87 
95 
     | 
    
         
             
            specification_version: 3
         
     | 
| 
       88 
96 
     | 
    
         
             
            summary: Searchable attributes
         
     |