Last active
January 6, 2016 08:24
-
-
Save garrow/227b5b391d9f16f29426 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# So I can access the following data only methods in both views and in class level validations, do I? | |
# e.g. | |
# validates :event_progress, inclusion: { in: event_progressions } | |
# | |
# = f.select :owner_relationship, f.object.owner_relationships, {}, class: "form-control" | |
######################################################################################################################## | |
# 1) I tried defining as instance methods & use procs to access in validations | |
# DOESNT WORK - undefined method `event_progressions' for Forms::WeddingMoodboard:Class | |
def owner_relationships | |
%w{ALPHA BRAVO CHARLIE DELTA} | |
end | |
def event_progressions | |
%w{ONE TWO THREE FOUR} | |
end | |
validates :event_progress, inclusion: { in: -> (_) { event_progressions } } | |
validates :owner_relationship, inclusion: { in: -> (_) { owner_relationships } } | |
######################################################################################################################## | |
# 2) Use class methods and create instance level helpers. | |
def self.owner_relationships | |
%w{ALPHA BRAVO CHARLIE DELTA} | |
end | |
def self.event_progressions | |
%w{ONE TWO THREE FOUR} | |
end | |
def owner_relationships | |
self.class.owner_relationships | |
end | |
def event_progressions | |
self.class.owner_relationships | |
end | |
# Use class methods in validations, and instance methods in the view! | |
validates :event_progress, inclusion: { in: event_progressions } | |
validates :owner_relationship, inclusion: { in: owner_relationships } | |
# = f.select :owner_relationship, f.object.owner_relationships | |
######################################################################################################################## | |
# OR 3) Use a module, and both include and extend the class with the module, so same effect | |
module SharedSelectionData | |
def owner_relationships | |
%w{ALPHA BRAVO CHARLIE DELTA} | |
end | |
def event_progressions | |
%w{ONE TWO THREE FOUR} | |
end | |
end | |
include SharedSelectionData | |
extend SharedSelectionData | |
#Again use class methods in validations, and instance methods in the view! | |
validates :event_progress, inclusion: { in: event_progressions } | |
validates :owner_relationship, inclusion: { in: owner_relationships } | |
# = f.select :owner_relationship, f.object.owner_relationships |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment