Class: Devise::MultiEmail::ParentModelManager
- Inherits:
-
Object
- Object
- Devise::MultiEmail::ParentModelManager
- Defined in:
- lib/devise/multi_email/parent_model_manager.rb
Instance Attribute Summary collapse
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Instance Method Summary collapse
- #change_primary_email_to(new_email) ⇒ Object
- #emails ⇒ Object
-
#filtered_emails ⇒ Object
Gets the email records that have not been deleted.
-
#initialize(record) ⇒ ParentModelManager
constructor
A new instance of ParentModelManager.
-
#primary_email ⇒ Object
Gets the primary email record.
Constructor Details
#initialize(record) ⇒ ParentModelManager
Returns a new instance of ParentModelManager.
9 10 11 |
# File 'lib/devise/multi_email/parent_model_manager.rb', line 9 def initialize(record) @record = record end |
Instance Attribute Details
#record ⇒ Object (readonly)
Returns the value of attribute record.
7 8 9 |
# File 'lib/devise/multi_email/parent_model_manager.rb', line 7 def record @record end |
Instance Method Details
#change_primary_email_to(new_email) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/devise/multi_email/parent_model_manager.rb', line 24 def change_primary_email_to(new_email) # Use Devise formatting settings for emails formatted_email = record.class.send(:devise_parameter_filter).filter(email: new_email)[:email] valid_emails = filtered_emails # mark none as primary when set to nil if new_email.nil? valid_emails.each{ |record| record.primary = false } # select or build an email record else record = valid_emails.find{ |record| record.email == formatted_email } unless record record = emails.build(email: formatted_email) valid_emails << record end # toggle the selected record as primary and others as not valid_emails.each{ |other| other.primary = (other == record) } end record end |
#emails ⇒ Object
50 51 52 |
# File 'lib/devise/multi_email/parent_model_manager.rb', line 50 def emails record.__send__(record.class.multi_email_association.name) end |
#filtered_emails ⇒ Object
Gets the email records that have not been deleted
14 15 16 |
# File 'lib/devise/multi_email/parent_model_manager.rb', line 14 def filtered_emails emails.reject(&:destroyed?).reject(&:marked_for_destruction?) end |
#primary_email ⇒ Object
Gets the primary email record.
19 20 21 |
# File 'lib/devise/multi_email/parent_model_manager.rb', line 19 def primary_email filtered_emails.find(&:primary?) end |