Class: ModelApi

Inherits:
ApplicationApi show all
Defined in:
lib/lux/api/lib/model_api.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.toggle_ids(name) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/lux/api/lib/model_api.rb', line 4

def toggle_ids name
  self.class_eval %[
    param :#{name}_id, Integer
    def toggle_#{name}
      message toggle!(@object, :#{name}_ids, @_#{name}_id) ? 'added' : 'removed'
    end
  ]
end

Instance Method Details

#createObject



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
# File 'lib/lux/api/lib/model_api.rb', line 43

def create
  @object = @class_name.constantize.new

  @last = @class_name.constantize.my.last rescue @class_name.constantize.last
  if @last && @last[:name].present? && @last[:name] == @params[:name]
    error "#{@class_name} is same as last one created."
  end

  for k,v in @params
    @object.send("#{k}=", v) if @object.respond_to?(k.to_sym)
  end

  can? :create, @object
  @object.save if @object.valid?
  return if report_errros_if_any @object

  if @object.id
    message '%s created' % @class_name.capitalize
  else
    error 'object not created, error unknown'
  end

  add_response_object_path

  attributes
end

#destroy(force: false) ⇒ Object

if you put active boolean field to objects, then they will be unactivated on destroy



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lux/api/lib/model_api.rb', line 92

def destroy force: false
  error "Object not found" unless @object
  can? :delete, @object

  if !force && @object.respond_to?(:is_active)
    @object.update is_active: false

    message 'Object deleted (exists in trashcan)'
  elsif !force && @object.respond_to?(:active)
    @object.update active: false

    message 'Object deleted (exists in trashcan)'
  else
    @object.destroy
    message '%s deleted' % @object.class.name
  end

  report_errros_if_any @object

  attributes
end

#indexObject



31
32
33
# File 'lib/lux/api/lib/model_api.rb', line 31

def index
  raise 'No index method defiend'
end

#showObject



35
36
37
38
39
40
41
# File 'lib/lux/api/lib/model_api.rb', line 35

def show
  error "Object not found" unless @object

  can? :read, @object

  attributes
end

#toggle!(object, field, value) ⇒ Object

toggles value in postgre array field



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lux/api/lib/model_api.rb', line 15

def toggle!(object, field, value)
  object[field] ||= []

  if object[field].include?(value)
    object[field] -= [value]
    object.save
    false
  else
    object[field] += [value]
    object.save
    true
  end
end

#undeleteObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/lux/api/lib/model_api.rb', line 114

def undelete
  error "Object not found" unless @object
  can? :create, @object

  if @object.respond_to?(:is_active)
    @object.update :is_active=>true
  elsif @object.respond_to?(:active)
    @object.update :active=>true
  else
    error "No is_active, can't undelete"
  end

  response.message = 'Object raised from the dead.'
end

#updateObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/lux/api/lib/model_api.rb', line 70

def update
  error "Object not found" unless @object

  for k,v in @params
    @object.send("#{k}=", v) if @object.respond_to?(k.to_sym)
  end

  can? :update, @object

  @object.updated_at = Time.now.utc if @object.respond_to?(:updated_at)
  @object.save if @object.valid?

  report_errros_if_any @object

  response.message '%s updated' % @class_name

  add_response_object_path

  attributes
end