Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #56 from jmonteiro/direct_name_association

Direct name association
  • Loading branch information...
commit e88f819dbc60a17143dcd3711b3065541a0a12c8 2 parents 43aff07 + b7330af
@jmonteiro jmonteiro authored
View
29 README.md
@@ -214,6 +214,35 @@ You can also use standard rails view helpers, like #collection_select:
<%= collection_select :person, :country_id, Country.all, :id, :name %>
+### Using shortcuts
+
+Since ActiveHashes usually are static, we can use shortcuts to assign via an easy to remember string instead of an obscure ID number.
+
+ # app/models/country.rb
+ class Country < ActiveHash::Base
+ end
+
+ # app/models/person.rb
+ class Person < ActiveRecord::Base
+ extend ActiveHash::Associations::ActiveRecordExtensions
+ belongs_to_active_hash :country, :shortcuts => [:name]
+ end
+
+ # config/initializers/data.rb
+ Country.data = [
+ {:id => 1, :name => "US"},
+ {:id => 2, :name => "Canada"}
+ ]
+
+ # Using `rails console`
+ john = Person.new
+ john.country_name = "US"
+ # Is the same as doing `john.country = Country.find_by_name("US")`
+ john.country_name
+ # Will return "US", and is the same as doing `john.country.try(:name)`
+
+You can have multiple shortcuts, so settings `:shortcuts => [:name, :friendly_name]` will enable you to use `#country_name=` and `#country_friendly_name=`.
+
## Referencing ActiveRecord objects from ActiveHash
If you include the ActiveHash::Associations module, you can also create associations from your ActiveHash classes, like so:
View
14 lib/associations/associations.rb
@@ -6,8 +6,10 @@ module ActiveRecordExtensions
def belongs_to_active_hash(association_id, options = {})
options = {
:class_name => association_id.to_s.camelize,
- :foreign_key => association_id.to_s.foreign_key
+ :foreign_key => association_id.to_s.foreign_key,
+ :shortcuts => []
}.merge(options)
+ options[:shortcuts] = [options[:shortcuts]] unless options[:shortcuts].kind_of?(Array)
define_method(association_id) do
options[:class_name].constantize.find_by_id(send(options[:foreign_key]))
@@ -17,6 +19,16 @@ def belongs_to_active_hash(association_id, options = {})
send "#{options[:foreign_key]}=", new_value ? new_value.id : nil
end
+ options[:shortcuts].each do |shortcut|
+ define_method("#{association_id}_#{shortcut}") do
+ send(association_id).try(shortcut)
+ end
+
+ define_method("#{association_id}_#{shortcut}=") do |new_value|
+ send "#{association_id}=", new_value ? options[:class_name].constantize.send("find_by_#{shortcut}", new_value) : nil
+ end
+ end
+
create_reflection(
:belongs_to,
association_id.to_sym,
View
18 spec/associations/associations_spec.rb
@@ -123,6 +123,24 @@ class Book < ActiveRecord::Base
school.city.should == city
end
+ it "is assignable by name attribute" do
+ School.belongs_to_active_hash :city, :shortcuts => [:name]
+ City.data = [ {:id => 1, :name => 'gothan'} ]
+ city = City.find_by_name 'gothan'
+ school = School.create :city_name => 'gothan'
+ school.city.should == city
+ school.city_name.should == 'gothan'
+ end
+
+ it "have custom shortcut" do
+ School.belongs_to_active_hash :city, :shortcuts => :friendly_name
+ City.data = [ {:id => 1, :friendly_name => 'Gothan City'} ]
+ city = City.find_by_friendly_name 'Gothan City'
+ school = School.create :city_friendly_name => 'Gothan City'
+ school.city.should == city
+ school.city_friendly_name.should == 'Gothan City'
+ end
+
it "returns nil when the record does not exist" do
School.belongs_to_active_hash :city
school = School.create! :city => nil
Please sign in to comment.
Something went wrong with that request. Please try again.