lib/puffer/resource.rb in puffer-0.0.5 vs lib/puffer/resource.rb in puffer-0.0.6

- old
+ new

@@ -14,15 +14,15 @@ attr_reader :request, :params, :prefix, :action, :controller_name, :model_name, :controller, :model def initialize params, request = nil @action = params.delete :action @controller = "#{params[:controller]}_controller".classify.constantize + @controller_name = controller.controller_name controller_segments = params.delete(:controller).split('/') @prefix = controller_segments.first - @controller_name = controller_segments.last - @model_name = (controller.current_config.model || controller_name.singularize).to_s - @model = model_name.classify.constantize + @model_name = controller.model_name + @model = controller.model @params = params @request = request end def plural? @@ -33,11 +33,11 @@ model.model_name.human end def parent @parent ||= begin - parent_ancestors = params[:ancestors].dup + parent_ancestors = params[:ancestors].dup rescue [] parent_name = parent_ancestors.pop if parent_name parent_params = ActiveSupport::HashWithIndifferentAccess.new({ :controller => "#{prefix}/#{parent_name.to_s.pluralize}", :action => 'index', @@ -50,11 +50,11 @@ key = ancestor.to_s.singularize.foreign_key parent_params.merge! key => params[key] if params[key] end parent_params.merge! :id => params[parent_name.to_s.singularize.foreign_key] - self.class.new parent_params, request + Resource.new parent_params, request else nil end end end @@ -68,10 +68,14 @@ end ancestors end end + def root + @root ||= (ancestors.first || self) + end + def children(custom_params = {}) @children ||= params[:children].map do |child_name| child_params = ActiveSupport::HashWithIndifferentAccess.new(custom_params.deep_merge({ :controller => "#{prefix}/#{child_name.to_s.pluralize}", :action => 'index', @@ -84,11 +88,11 @@ key = ancestor.to_s.singularize.foreign_key child_params.merge! key => params[key] if params[key] end child_params.merge! controller_name.singularize.foreign_key => params[:id] if params[:id] - self.class.new child_params, request + Resource.new child_params, request end end def collection scope = parent ? parent.member.send(model_name.pluralize) : model @@ -118,23 +122,19 @@ model.new attributes end end def attributes - params[model_name] || {} + params[model_name] end - def template suggest = nil - "puffer/#{suggest || action}" - end - def method_missing method, *args, &block method = method.to_s - if method.match(/path$/) + if method.match(/path$/) && respond_to?(method.gsub(/path$/, 'url')) options = args.extract_options! - return send method.gsub(/path$/, 'url'), *(args << options.merge(:routing_type => :path)) if defined? method.gsub(/path$/, 'url') + return send method.gsub(/path$/, 'url'), *(args << options.merge(:routing_type => :path)) end - model.send method, *args, &block + model.send method, *args, &block if model.respond_to? method end end end