Class: Auth::AuthenticatedController

Inherits:
ApplicationController show all
Includes:
Concerns::DeviseConcern, Concerns::TokenConcern
Defined in:
app/controllers/auth/authenticated_controller.rb

Constant Summary collapse

CONDITIONS_FOR_TOKEN_AUTH =
[:create,:update,:destroy,:edit,:new,:index]
TCONDITIONS =
{:only => CONDITIONS_FOR_TOKEN_AUTH}

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_resource!, #check_for_create, #check_for_destroy, #check_for_update, #from_bson, #from_view, #not_found

Instance Method Details

#build_model_from_paramsObject



62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/auth/authenticated_controller.rb', line 62

def build_model_from_params
      pp = permitted_params
      puts "the permitted_params are:"
      puts permitted_params.to_s

      @model_params = pp.fetch(get_model_class_name.to_sym,{})
      puts "model params are:"
      puts @model_params.to_s

      @model = pp[:id] ?  @model_class.find_self(pp[:id],current_signed_in_resource) : @model_class.new(@model_params)

end

#createObject

POST /auth/assemblies



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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/controllers/auth/authenticated_controller.rb', line 118

def create

## so how to create an assemly
## there is no difference.

## how to create a stage
## we need to know the assembly version
## and find one and update, where id is that and its version is that.

## we can specify those conditions on the model.
## for example we can have a method called model_create
## and return the model from that.

  respond_to do |format|
      if @model.create_with_conditions(params,@model_params,@model)
          format.json do 
              render json: @model.to_json, status: 201
          end
          format.html do 
              render :show
          end
          format.text do 
              render :text => @model.text_representation 
          end
          format.js do 
              render :partial => "show.js.erb", locals:{model: @model}
          end
      else
       format.json do 
           render json: {
             id: @model.id.to_s,
             errors: @model.errors
           }.to_json, status: 422
       end
       format.html do 
           render :new
       end
       format.js do 
           render :partial => "show.js.erb", locals:{model: @model}
       end
      end
  end
end

#destroyObject

DELETE /auth/assemblies/1



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/controllers/auth/authenticated_controller.rb', line 193

def destroy
    respond_to do |format|
      if @model.destroy
        format.json do 
          render :nothing => true, :status => 204
        end
      else
        format.json do 
          render json: {
            id: @model.id.to_s,
            errors: @model.errors
          }.to_json
        end
      end
    end
end

#editObject

GET /auth/assemblies/1/edit



114
115
# File 'app/controllers/auth/authenticated_controller.rb', line 114

def edit
end

#get_model_class_nameObject

@return model_name : given a controller with name AssembliesController -> will return assembly will downcase and singularize the controller name.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/auth/authenticated_controller.rb', line 23

def get_model_class_name
  
  class_name = nil

  self.class.name.scan(/::(?<plural_controller_name>[A-Za-z]+)Controller$/) do |ll|

    jj = Regexp.last_match
    
    plural_controller_name = jj[:plural_controller_name]

    class_name = plural_controller_name.singularize.downcase

  end

  not_found("could not determine class name") unless class_name
  
  

  return class_name

end

#indexObject

GET /auth/assemblies



87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/auth/authenticated_controller.rb', line 87

def index
    @models = @model.get_many
    respond_to do |format|
      format.json do 
        render json: @models.to_json
      end
      format.html do 
        render :index
      end
    end
end

#instantiate_classesObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/auth/authenticated_controller.rb', line 45

def instantiate_classes

  if Auth.configuration.send("#{get_model_class_name}_class")

    begin
      instance_variable_set("@model_class",Auth.configuration.send("#{get_model_class_name}_class").constantize)
    rescue 
      not_found("could not instantiate class #{get_model_class_name}")
    end

  else
    not_found("#{get_model_class_name} class not defined in configuration")
  end

end

#newObject

GET /auth/assemblies/new



109
110
111
# File 'app/controllers/auth/authenticated_controller.rb', line 109

def new
  #@auth_assembly = Auth::Assembly.new
end

#onlyObject

add the filters for check_for_create, check_for_update and check_for_destroy



14
# File 'app/controllers/auth/authenticated_controller.rb', line 14

before_filter(:only => [:create]){|c| check_for_create(@model)}

#showObject

GET /auth/assemblies/1



100
101
102
103
104
105
106
# File 'app/controllers/auth/authenticated_controller.rb', line 100

def show
    respond_to do |format|
      format.json do 
        render json: @model.to_json
      end
    end
end

#updateObject

PATCH/PUT /auth/assemblies/1



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/controllers/auth/authenticated_controller.rb', line 163

def update
  respond_to do |format|
      if @model.update_with_conditions(params,@model_params,@model)
        format.json do 
            render :nothing => true, :status => 204
        end
        format.js do 
          render :partial => "show.js.erb", locals: {model: @model}
        end
        format.html do 
          render :show
        end
      else
        format.json do 
            render json: {
              id: @model.id.to_s,
              errors: @model.errors
            }.to_json, status: 422
        end
        format.js do 
          render :partial => "show.js.erb", locals: {model: @model}
        end
        format.html do 
          render :show
        end
      end
  end
end