Module: LookupCommon

Instance Method Summary collapse

Instance Method Details

#createObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/concerns/lookup_common.rb', line 17

def create
  clazz = get_model
  obj = clazz.new(lookup_params)
  class_name = get_model_name.split('::').last.underscore.humanize
  if obj.save
    response = Mks::Common::MethodResponse.new(true, "#{class_name} saved successfully!",obj, nil, nil)
  else
    errors = Mks::Common::Util.error_messages obj, class_name
    response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil)
  end
  render json: response
end

#get_model(type = nil) ⇒ Object



41
42
43
44
45
46
# File 'app/controllers/concerns/lookup_common.rb', line 41

def get_model(type = nil)
  if type
    return type.constantize
  end
  get_model_name.constantize
end

#get_model_nameObject



48
49
50
51
52
# File 'app/controllers/concerns/lookup_common.rb', line 48

def get_model_name
  name = self.class.to_s
  name.slice!('Controller')
  name.singularize
end

#indexObject



10
11
12
13
14
15
# File 'app/controllers/concerns/lookup_common.rb', line 10

def index
  p = params[:type]
  clazz = get_model(p)
  response = Mks::Common::MethodResponse.new(true, nil, clazz.all, nil, nil)
  render json: response
end

#updateObject



30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/concerns/lookup_common.rb', line 30

def update
  class_name = get_model_name.split('::').last.underscore.humanize
  if @lookup.update(lookup_params)
    response = Mks::Common::MethodResponse.new(true, "#{class_name} updated successfully!", @lookup, nil, nil)
  else
    errors = Mks::Common::Util.error_messages @lookup, class_name
    response = Mks::Common::MethodResponse.new(false, nil, nil, errors, nil)
  end
  render json: response
end