Skip to content

Instantly share code, notes, and snippets.

@bradgessler
Last active October 25, 2022 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradgessler/e38c4329460a2c0c7048eccc34ab2ff1 to your computer and use it in GitHub Desktop.
Save bradgessler/e38c4329460a2c0c7048eccc34ab2ff1 to your computer and use it in GitHub Desktop.
action classes
module Resources
class BaseAction
attr_accessor :params
def before
end
def after
end
def render
end
def request
before
render
after
end
def self.request(params)
action = new
action.params = params
action.request
end
end
class Resources < BaseAction
def before
super
authenticate
end
def authenticate
raise "Inauthentic" if current_user.nil?
end
def current_user
params[:user_id]
end
def scope
raise "Put an active model in here"
end
Index = self
class New < self
def resource
super.build
end
end
class Create < self
def resource
super.build(params)
end
end
class Show < self
def scope
super.find(params[:id])
end
def before
super
puts"Do something special with #{params.fetch(:id)}"
end
def render
puts "Hello from user #{params.fetch(:id)}"
end
end
class Edit < Show
end
class Destroy < Show
end
end
class Posts < Resources
def render
puts "Rendering all the posts for #{current_user}"
end
class Show < self
end
end
end
# We'd actually pass a Request object into this.
Resources::Posts.request(user_id: 100)
Resources::Posts::Show.request(user_id: 100, id: 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment