Module: Hobo::Controller::Model::ClassMethods
- Defined in:
- lib/hobo/controller/model.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #auto_actions(*args) ⇒ Object
- #auto_actions_for(owner, actions) ⇒ Object
- #autocomplete(*args, &block) ⇒ Object
- #available_auto_actions ⇒ Object
- #available_auto_lifecycle_actions ⇒ Object
- #available_auto_read_actions ⇒ Object
- #available_auto_write_actions ⇒ Object
- #creator_page_action(name, options = {}, &block) ⇒ Object
- #def_auto_action(name, &block) ⇒ Object
- #def_auto_actions ⇒ Object
- #def_lifecycle_actions ⇒ Object
- #do_creator_action(name, options = {}, &block) ⇒ Object
- #do_transition_action(name, options = {}, &block) ⇒ Object
- #include_action?(name) ⇒ Boolean
- #index_action(*names, &block) ⇒ Object
- #model_name ⇒ Object
- #show_action(*names, &block) ⇒ Object
- #transtion_page_action(name, options = {}, &block) ⇒ Object
- #web_method(web_name, options = {}, &block) ⇒ Object
Instance Attribute Details
#model ⇒ Object
94 95 96 |
# File 'lib/hobo/controller/model.rb', line 94 def model @model ||= controller_name.camelcase.singularize.constantize end |
Instance Method Details
#auto_actions(*args) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/hobo/controller/model.rb', line 133 def auto_actions(*args) = args. @auto_actions = args.map do |arg| case arg when :all then available_auto_actions when :write_only then available_auto_write_actions when :read_only then available_auto_read_actions when :lifecycle then available_auto_lifecycle_actions else arg end end.flatten.uniq except = Array([:except]) except_actions = except.map do |arg| case arg when :lifecycle then available_auto_lifecycle_actions else arg end end.flatten.uniq @auto_actions -= except_actions def_auto_actions end |
#auto_actions_for(owner, actions) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/hobo/controller/model.rb', line 271 def auto_actions_for(owner, actions) name = model.reflections[owner.to_s].macro == :has_many ? owner.to_s.singularize : owner owner_actions[owner] ||= [] Array(actions).each do |action| case action when :new define_method("new_for_#{name}") { hobo_new_for owner } when :index define_method("index_for_#{name}") { hobo_index_for owner } when :create define_method("create_for_#{name}") { hobo_create_for owner } else raise ArgumentError, "Invalid owner action: #{action}" end owner_actions[owner] << action end end |
#autocomplete(*args, &block) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/hobo/controller/model.rb', line 99 def autocomplete(*args, &block) = args. name = args.first || model.name_attribute field = .delete(:field) || name if block index_action "complete_#{name}", &block else index_action "complete_#{name}" do hobo_completions field, model, end end end |
#available_auto_actions ⇒ Object
296 297 298 299 300 301 |
# File 'lib/hobo/controller/model.rb', line 296 def available_auto_actions (available_auto_read_actions + available_auto_write_actions + FORM_ACTIONS + available_auto_lifecycle_actions).uniq end |
#available_auto_lifecycle_actions ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/hobo/controller/model.rb', line 318 def available_auto_lifecycle_actions # For each creator/transition there are two possible # actions. e.g. for signup, 'signup' would be routed to # GET users/signup, and would show the form, while 'do_signup' # would be routed to POST /users/signup) if model.has_lifecycle? (model::Lifecycle.publishable_creators.map { |c| [c.name, "do_#{c.name}"] } + model::Lifecycle.publishable_transitions.map { |t| [t.name, "do_#{t.name}"] }).flatten.*.to_sym else [] end end |
#available_auto_read_actions ⇒ Object
304 305 306 |
# File 'lib/hobo/controller/model.rb', line 304 def available_auto_read_actions READ_ONLY_ACTIONS end |
#available_auto_write_actions ⇒ Object
309 310 311 312 313 314 315 |
# File 'lib/hobo/controller/model.rb', line 309 def available_auto_write_actions if model.method_defined?("position_column") WRITE_ONLY_ACTIONS + [:reorder] else WRITE_ONLY_ACTIONS end end |
#creator_page_action(name, options = {}, &block) ⇒ Object
243 244 245 246 247 |
# File 'lib/hobo/controller/model.rb', line 243 def creator_page_action(name, ={}, &block) define_method(name) do creator_page_action name, , &block end end |
#def_auto_action(name, &block) ⇒ Object
179 180 181 |
# File 'lib/hobo/controller/model.rb', line 179 def def_auto_action(name, &block) define_method name, &block if !method_defined?(name) && include_action?(name) end |
#def_auto_actions ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/hobo/controller/model.rb', line 160 def def_auto_actions self.class_eval do def index; hobo_index end if include_action?(:index) def show; hobo_show end if include_action?(:show) def new; hobo_new end if include_action?(:new) def create; hobo_create end if include_action?(:create) def edit; hobo_show end if include_action?(:edit) def update; hobo_update end if include_action?(:update) def destroy; hobo_destroy end if include_action?(:destroy) def completions; hobo_completions end if include_action?(:completions) def reorder; hobo_reorder end if include_action?(:reorder) end def_lifecycle_actions end |
#def_lifecycle_actions ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/hobo/controller/model.rb', line 184 def def_lifecycle_actions if model.has_lifecycle? model::Lifecycle.publishable_creators.each do |creator| name = creator.name def_auto_action name do creator_page_action name end def_auto_action "do_#{name}" do do_creator_action name end end model::Lifecycle.publishable_transitions.each do |transition| name = transition.name def_auto_action name do transition_page_action name end def_auto_action "do_#{name}" do do_transition_action name end end end end |
#do_creator_action(name, options = {}, &block) ⇒ Object
250 251 252 253 254 |
# File 'lib/hobo/controller/model.rb', line 250 def do_creator_action(name, ={}, &block) define_method("do_#{name}") do do_creator_action name, , &block end end |
#do_transition_action(name, options = {}, &block) ⇒ Object
264 265 266 267 268 |
# File 'lib/hobo/controller/model.rb', line 264 def do_transition_action(name, ={}, &block) define_method("do_#{name}") do do_transition_action name, , &block end end |
#include_action?(name) ⇒ Boolean
291 292 293 |
# File 'lib/hobo/controller/model.rb', line 291 def include_action?(name) name.to_sym.in?(@auto_actions) end |
#index_action(*names, &block) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/hobo/controller/model.rb', line 222 def index_action(*names, &block) = names. index_actions.concat(names) for name in names if block define_method(name, &block) else if scope = .delete(:scope) if scope.is_a?(Symbol) define_method(name) { hobo_index model.send(scope), .dup } else define_method(name) { hobo_index scope, .dup } end else define_method(name) { hobo_index .dup } end end end end |
#model_name ⇒ Object
90 91 92 |
# File 'lib/hobo/controller/model.rb', line 90 def model_name model.name.underscore end |
#show_action(*names, &block) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/hobo/controller/model.rb', line 209 def show_action(*names, &block) = names. show_actions.concat(names) for name in names if block define_method(name, &block) else define_method(name) { hobo_show .dup } end end end |
#transtion_page_action(name, options = {}, &block) ⇒ Object
257 258 259 260 261 |
# File 'lib/hobo/controller/model.rb', line 257 def transtion_page_action(name, ={}, &block) define_method(name) do transtion_page_action name, , &block end end |
#web_method(web_name, options = {}, &block) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/hobo/controller/model.rb', line 113 def web_method(web_name, ={}, &block) web_methods << web_name.to_sym method = .delete(:method) || web_name got_block = block_given? define_method web_name do # Make sure we have a copy of the options - it is being mutated somewhere opts = .dup self.this = find_instance(opts) raise Hobo::PermissionDeniedError unless @this.method_callable_by?(current_user, method) if got_block this.with_acting_user(current_user) { instance_eval(&block) } else @this.send(method) end hobo_ajax_response unless performed? end end |