Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. module Inquiry
  2. class FilledByRegularUser
  3. include Virtus.model
  4.  
  5. attribute :first_name, String
  6. attribute :last_name, String
  7. attribute :email, String
  8.  
  9. # validate email if filled by regular user
  10. validates :email, presence: true
  11. end
  12. end
  13.  
  14. module Inquiry
  15. class FilledByAdminUser
  16. include Virtus.model
  17.  
  18. attribute :first_name, String
  19. attribute :last_name, String
  20. attribute :email, String
  21.  
  22. # no e-mail validation required
  23. end
  24. end
  25.  
  26. class InquriesController < ApplicationController
  27. def inquiry_handler
  28. Inquiry::Handler.new(@inquiry, inquiry_data)
  29. end
  30.  
  31. def inquiry_data
  32. Inquiry::FilledByRegularUser.new(inquiry_params)
  33. end
  34. end
  35.  
  36. class InquriesAdminController < ApplicationController
  37. def inquiry_handler
  38. Inquiry::Handler.new(@inquiry, inquiry_data)
  39. end
  40.  
  41. def inquiry_data
  42. Inquiry::FilledByAdminUser.new(inquiry_params)
  43. end
  44. end
  45.  
  46. module Inquiry
  47. class Handler
  48. attr_reader :record, :record_data
  49.  
  50. def initialize(record, record_data)
  51. @record = record
  52. @record_data = record_data
  53. end
  54.  
  55. def call
  56. if valid?
  57. assign_attributes
  58. record.save(validate: false)
  59. Inquiry::Mailer.notification(record).deliver_later
  60. Broadcaster.call(record)
  61. return true
  62. else
  63. return false
  64. end
  65. end
  66.  
  67. private
  68. delegate :valid?, :attributes, to: :record_data
  69.  
  70. def assign_attributes
  71. record.assign_attributes(attributes)
  72. end
  73. end
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement