Skip to content

Instantly share code, notes, and snippets.

@danshultz
Created November 5, 2012 22:30
Show Gist options
  • Select an option

  • Save danshultz/4020833 to your computer and use it in GitHub Desktop.

Select an option

Save danshultz/4020833 to your computer and use it in GitHub Desktop.
Thoughts on Decorator
class SomeController < AC::Base
# defines the general view model along with a way to introduce the state and pre-scope models
view_model = MyGeneralViewModel do |state|
user = get_user
state.user = user
state.scope = Model.where(:user => user)
end
# interact with the view model just like an active record model
# scopes would be present and things would feel like AR
# All instances returned would be wrapped in an instance of the decorator with
# the declared state introduced. It would be like instantiating a view model with...
#
# ViewModel.new(ar_model, user, other_data)
def index
items = view_model.where(:field_1 => params[:field_1], :field_2 => params[:field_2])
render items
end
def index
item = view_model.find(params[:id])
render item
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment