Guest User

Untitled

a guest
Apr 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. module RestfulScaffold
  2.  
  3. def self.included(controller)
  4. controller.extend ClassMethods
  5. end
  6.  
  7. module ClassMethods
  8. def rest_methods_for(table_name, *args)
  9. module_eval do
  10. after_filter :standard_respond_to, :only => [ :index, :show, :new ]
  11. after_filter :set_friendly_instance_variable
  12.  
  13. # if table_name == :people; return People
  14. define_method :model_class do
  15. table_name.to_s.classify.constantize
  16. end
  17.  
  18. # if table_name == :people; return "person"
  19. define_method :friendly_model_var_name do
  20. table_name.to_s.singularize
  21. end
  22.  
  23. # if table_name == :people; return "people"
  24. define_method :friendly_model_var_name_plural do
  25. table_name.to_s
  26. end
  27.  
  28. define_method :set_friendly_instance_variable do
  29. if defined?(@model)
  30. instance_variable_set("@#{friendly_model_var_name}", @model)
  31. elsif defined?(@models)
  32. instance_variable_set("@#{friendly_model_var_name_plural}", @models)
  33. end
  34. end
  35.  
  36. # if table_name == :people, return result of people_path
  37. define_method :friendly_model_index_path do
  38. eval "#{table_name}_path"
  39. end
  40.  
  41. include RestMethods
  42. end
  43. end
  44. end
  45.  
  46. module RestMethods
  47. def index
  48. @models = model_class.find(:all)
  49. end
  50.  
  51. def show
  52. @model = model_class.find(params[:id])
  53. end
  54.  
  55. def new
  56. @model = model_class.new
  57. end
  58.  
  59. def edit
  60. @model = model_class.find(params[:id])
  61. end
  62.  
  63. def create
  64. @model = model_class.new(params[friendly_model_var_name])
  65. respond_to do |format|
  66. if @model.save
  67. flash[:notice] = "#{friendly_model_var_name.humanize} was successfully updated."
  68. format.html { redirect_to(self.instance_variable_get("@#{singular}")) }
  69. format.xml { head :ok }
  70. format.json { render :json => @model.to_json }
  71. else
  72. format.html { render :action => "edit" }
  73. format.xml { render :xml => @model.errors, :status => :unprocessable_entity }
  74. format.json { render :json => @model, :status => :unprocessable_entity }
  75. end
  76. end
  77. end
  78.  
  79. def update
  80. @model = model_class.find(params[:id])
  81. respond_to do |format|
  82. if @model.update_attributes(params[friendly_model_var_name])
  83. flash[:notice] = "#{friendly_model_var_name.humanize} was successfully updated."
  84. format.html { redirect_to(@model) }
  85. format.xml { head :ok }
  86. format.json { render :json => @model.to_json }
  87. else
  88. format.html { render :action => "edit" }
  89. format.xml { render :xml => @model.errors, :status => :unprocessable_entity }
  90. format.json { render :json => @model, :status => :unprocessable_entity }
  91. end
  92. end
  93. end
  94.  
  95. def destroy
  96. @model = model_class.find(params[:id])
  97. @model.destroy
  98. respond_to do |format|
  99. format.html { redirect_to( friendly_model_index_path ) }
  100. format.xml { head :ok }
  101. format.json { head :ok }
  102. end
  103. end
  104.  
  105. private
  106.  
  107. def standard_respond_to
  108. respond_to do |format|
  109. format.html
  110. format.xml { render :xml => @model }
  111. format.json { render :json => @model }
  112. end
  113. end
  114. end
  115. end
  116.  
  117. # Automatically include it so we only have to use the
  118. # macro-style declaration to get the restful goodness
  119. class ActionController::Base
  120. include RestfulScaffold
  121. end
Add Comment
Please, Sign In to add comment