Module: Hopsoft::ActsAsLookup::IsLookup::StaticMethods
- Defined in:
- lib/hopsoft/acts_as_lookup.rb
Overview
Add class methods here
Instance Attribute Summary collapse
-
#key_column ⇒ Object
readonly
Returns the value of attribute key_column.
Instance Method Summary collapse
-
#get_column_value(value, column_name) ⇒ Object
Gets a column’s value from an “acts_as_lookup” model.
-
#get_record(value) ⇒ Object
Gets an ActiveRecord object from an “acts_as_lookup” model.
-
#init ⇒ Object
Initializes the plugin.
- #method_missing(name, *args) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/hopsoft/acts_as_lookup.rb', line 148 def method_missing(name, *args) if args && args.length == 1 column_name = args[0].to_s.downcase if column_name =~ /^object$/ return get_record(name) else if @lookup_columns.include?(column_name) # once here we are assuming they are performing a lookup return get_column_value(name, column_name) end end end return super end |
Instance Attribute Details
#key_column ⇒ Object (readonly)
Returns the value of attribute key_column.
146 147 148 |
# File 'lib/hopsoft/acts_as_lookup.rb', line 146 def key_column @key_column end |
Instance Method Details
#get_column_value(value, column_name) ⇒ Object
Gets a column’s value from an “acts_as_lookup” model.
Params
-
value - The “key column” value to find.
-
column_name - The column’s name to return a value for.
171 172 173 174 175 |
# File 'lib/hopsoft/acts_as_lookup.rb', line 171 def get_column_value(value, column_name) record = get_record(value) return record.attributes[column_name] if record return nil end |
#get_record(value) ⇒ Object
Gets an ActiveRecord object from an “acts_as_lookup” model.
Params
-
value - The “key column” value to find.
181 182 183 184 185 186 |
# File 'lib/hopsoft/acts_as_lookup.rb', line 181 def get_record(value) value = value.to_s.downcase.gsub(/_/, " ") finder_method = "find_by_#{@key_column}" item = send(finder_method, value) item end |
#init ⇒ Object
Initializes the plugin.
189 190 191 192 |
# File 'lib/hopsoft/acts_as_lookup.rb', line 189 def init @lookup_columns = new.attributes.keys.map {|attr_name| attr_name.downcase.gsub(/ /, "_") } @lookup_columns << "id" end |