code_junkie

Does passing the params collection from controller to model break MVC?

Nov 12th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. class Model < ActiveRecord::Base
  2. def update_from_params(params)
  3. ....
  4. end
  5. end
  6.  
  7. class ModelsController < ActionController::Base
  8. def update
  9. ...
  10. @model.update_from_params(params)
  11. end
  12. end
  13.  
  14. class Model < ActiveRecord::Base
  15. def update_from_update_params(params)
  16. # do stuff
  17. end
  18.  
  19. def update_from_settings_params(params)
  20. # do different stuff
  21. end
  22.  
  23. end
  24.  
  25. class ModelsController < ActionController::Base
  26. def update
  27. ...
  28. @model.update_from_update_params(params)
  29. end
  30.  
  31. def change_settings
  32. ...
  33. @model.update_from_settings_params(params)
  34. end
  35. end
  36.  
  37. class Model < ActiveRecord::Base
  38. def update_from_data(hash)
  39. validate_data!(hash)
  40. # do stuff
  41. end
  42. end
  43.  
  44. class ModelsController < ActionController::Base
  45. def update
  46. ...
  47. @model.update_from_data(translate_update_params)
  48. end
  49.  
  50. def change_settings
  51. ...
  52. @model.update_from_data(translate_change_settings_params)
  53. end
  54. end
  55.  
  56. # in controller
  57. def search
  58. Model.search(params[:search][:options])
  59. end
  60.  
  61. <!-- in view -->
  62. <input type='text' name='search[options][keywords]' />
  63. <input type='text' name='search[options][conditions]' />
  64. <input type='text' name='search[options][sort]' />
  65.  
  66. def self.do_search(criteria)
  67.  
  68. Rental.search(criteria[:keywords],
  69. :per_page => self.per_page,
  70. :page => page,
  71. :conditions => criteria[:conditions],
  72. :order => criteria[:sort])
  73.  
  74. end
  75.  
  76. def self.do_search(keyword,params)
  77.  
  78. Rental.search(keyword,
  79. :per_page => self.per_page,
  80. :page => page,
  81. :conditions => will_condition(params),
  82. :order => will_sort(params))
  83.  
  84. end
  85.  
  86. @@searchable_fields = ['property_status_id', 'neighborhood_id', 'borough_id', 'number_of_bedrooms']
  87. cattr_reader :searchable_fields
  88.  
  89. @@sortable_fields = ['number_of_bedrooms','rent','street_address']
  90. cattr_reader :sortable_fields
Add Comment
Please, Sign In to add comment