Class: Jason::JasonController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/jason/jason_controller.rb

Instance Method Summary collapse

Instance Method Details

#actionObject



22
23
24
25
26
27
28
29
30
31
32
33
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
# File 'app/controllers/jason/jason_controller.rb', line 22

def action
  type = params[:type]
  entity = type.split('/')[0].underscore
  model_name = entity.singularize
  api_model = Jason::ApiModel.new(model_name)
  model = model_name.camelize.constantize
  action = type.split('/')[1].underscore

  if action == 'move_priority'
    id, priority = params[:payload].values_at(:id, :priority)

    instance = model.find(id)

    return head :forbidden if !action_permitted?(model_name, action, instance, params)
    priority_filter = instance.as_json.with_indifferent_access.slice(*api_model.priority_scope)

    all_instance_ids = model.send(api_model.scope || :all).where(priority_filter).where.not(id: instance.id).order(:priority).pluck(:id)
    all_instance_ids.insert(priority.to_i, instance.id)

    all_instance_ids.each_with_index do |id, i|
      model.find(id).update!(priority: i)
    end

    model.find(all_instance_ids).each(&:force_publish_json)
  elsif action == 'upsert' || action == 'add'
    id = params[:payload][:id]
    payload = api_model.permit(params)

    instance = model.find_by(id: id)
    return head :forbidden if !action_permitted?(model_name, action, instance, params)

    if instance.present?
      instance.update!(payload)
    else
      instance = model.create!(payload)
    end

    return render json: instance.as_json(api_model.as_json_config)
  elsif action == 'remove'
    instance = model.find(params[:payload])
    return head :forbidden if !action_permitted?(model_name, action, instance, params)

    instance.destroy!
  end

  return head :ok
end

#configurationObject

config seems to be a reserved name, resulting in infinite loop



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/jason/jason_controller.rb', line 4

def configuration
  payload = {
    schema: Jason.schema,
    transportService: Jason.transport_service,
  }

  if Jason.transport_service == :pusher
    payload.merge!({
      pusherKey: Jason.pusher_key,
      pusherRegion: Jason.pusher_region,
      pusherHost: Jason.pusher_host || nil,
      pusherChannelPrefix: Jason.pusher_channel_prefix
    })
  end

  render json: payload
end

#create_subscriptionObject



70
71
72
73
# File 'app/controllers/jason/jason_controller.rb', line 70

def create_subscription
  @subscription.add_consumer(params[:consumer_id])
  render json: { channelName: @subscription.channel }
end

#get_payloadObject



79
80
81
82
83
84
85
# File 'app/controllers/jason/jason_controller.rb', line 79

def get_payload
  if params[:options].try(:[], :force_refresh)
    @subscription.set_ids_for_sub_models
  end

  render json: @subscription.get
end

#remove_subscriptionObject



75
76
77
# File 'app/controllers/jason/jason_controller.rb', line 75

def remove_subscription
  @subscription.remove_consumer(params[:consumer_id])
end