Module: ActionController::ModifyResource

Includes:
ResourceInfo, ActionView::Helpers::TextHelper
Defined in:
lib/modify_resource/rails/action_controller/router.rb,
lib/modify_resource/rails/action_controller/resource_info.rb,
lib/modify_resource/rails/action_controller/modify_resource.rb

Defined Under Namespace

Modules: ClassMethods, ResourceInfo Classes: Router

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ResourceInfo

#model_class, #resource, #resource_name

Class Method Details

.included(base) ⇒ Object

:nodoc:



28
29
30
# File 'lib/modify_resource/rails/action_controller/modify_resource.rb', line 28

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#modify_resource_with(*args) ⇒ Object

Handles the usual @var = Model.find(params); @var.save etc. stuff.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/modify_resource/rails/action_controller/modify_resource.rb', line 34

def modify_resource_with(*args) # ([res=nil, ], verb=:update, options={}, args=nil)
  # Set var to resource matching controller's name if res is a symbol
  if args.first.is_a? Symbol or args.first.is_a? String
    res = instance_variable_get(:"@#{resource_name}")
  else
    res = args.shift
  end
  
  # Set up default values
  verb      = args[0] || :update
  options   = args[1] || {}
  _params   = args[2] || nil
  as_user   = nil
  
  # model_name is this res's model_name
  model_name = res.class.model_name.underscore.downcase
  
  # Grab params if not set
  _params ||= params[model_name]
  
  # We may need the current user instance
  options[:as_current] ||= options[:as_user] # Backwards compat
  
  if options[:as_current] and res.respond_to?(:as_user)
    res.as_user = self.send "current_#{options[:as_current]}"
  end
  
  # Actually perform update, or create, destroy, etc.
  modified = case verb
    when :update
      res.update_permitted_attributes(_params)
    when :create
      res.deep_attributes = _params
      res.persisted?
    else
      res.method(verb).arity == 0 ? res.send(verb) : res.send(verb, _params)
    end
    
  # the resource was updated
  instance_variable_set :"@#{resource_name}", res
  
  raise 'As_user MUST be unset on ALL items.' if res.valid? and res.as_user.present?
  
  # We're updating a nested resource, so we need to set its parent for it
  update_resource_parent(res) unless parent_for(res).present?
  
  # Actually save or create.
  if modified
    
    unless request.xhr?
      
      options[:redirect_with_resources] ||= [ :self ]
      after = params[:after]
      unless (path = options[:redirect_to]).blank?
        router = Router.new
        success_path = if options[:redirect_with_resources].blank?
          router.send path
        else
          router.send path, *collect_resources_for(res, options[:redirect_with_resources])
        end
      end
      success_path ||= resource_path_with_base(:production, res)
      
      msg = options[:success].try(:call, res)
      
      if msg.blank?
        
        msg = "#{action_name.capitalize.gsub(/e$/,'')}ed "
        
        if after.present? and after.pluralize == after
          # Redirect to the plural (index) page
          model_name = params[:after]
          msg << model_name.pluralize
          success_path << '/' + after
        else
          # Redirect to the show page for the resource.
          # Flash is like 'Widget 2 has been updated'.
          name = String.new.tap do |s|
            break s = res.title if res.respond_to? :title
            break s = res.name if res.respond_to? :name
            s = res.id
          end
          msg << "#{model_name.humanize.downcase} '#{truncate(name, length: 20)}'"
        end
      end
      
      flash[:notice] = msg
      redirect_to success_path
    else
      render json: res
    end
  else
    messages = res.errors.messages
    unless request.xhr?
      flash[:error] = messages
      begin
        render :edit
      rescue
        # They didn't respond to that action
        if res.persisted?
          render :show
        else
          render :new
        end
      end
    else
      render json: {errors: messages}, status: :unprocessable_entity
    end
  end
end