Module: Coupler::Extensions::Transformations

Defined in:
lib/coupler/extensions/transformations.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
# File 'lib/coupler/extensions/transformations.rb', line 4

def self.registered(app)
  app.get "/projects/:project_id/resources/:resource_id/transformations" do
    @resource = @project.resources_dataset[:id => params[:resource_id]]
    raise ResourceNotFound  unless @resource
    @transformations = @resource.transformations
    erb('transformations/index'.to_sym)
  end

  app.get "/projects/:project_id/resources/:resource_id/transformations/new" do
    @resource = @project.resources_dataset[:id => params[:resource_id]]
    raise ResourceNotFound  unless @resource
    @fields = @resource.selected_fields_dataset.order(:id).all
    @transformers = Models::Transformer.all
    @transformation = Models::Transformation.new()
    erb 'transformations/new'.to_sym
  end

  app.post "/projects/:project_id/resources/:resource_id/transformations" do
    @resource = @project.resources_dataset[:id => params[:resource_id]]
    raise ResourceNotFound  unless @resource
    @fields = @resource.selected_fields_dataset.order(:id).all
    @transformation = Models::Transformation.new(params[:transformation])
    @transformation.resource = @resource

    if @transformation.save
      flash[:notice] = "Transformation was successfully created."
      redirect "/projects/#{@project.id}/resources/#{@resource.id}"
    else
      @transformers = Models::Transformer.all
      @preview = @resource.preview_transformation(@transformation)
      erb :'transformations/create'
    end
  end

  app.delete "/projects/:project_id/resources/:resource_id/transformations/:id" do
    @resource = @project.resources_dataset[:id => params[:resource_id]]
    raise ResourceNotFound  unless @resource
    @transformation = @resource.transformations_dataset[:id => params[:id]]
    raise TransformationNotFound  unless @transformation
    @transformation.destroy
    redirect "/projects/#{@project.id}/resources/#{@resource.id}"
  end

  app.get "/projects/:project_id/resources/:resource_id/transformations/for/:field_name" do
    @resource = @project.resources_dataset[:id => params[:resource_id]]
    raise ResourceNotFound  unless @resource
    @field = @resource.fields_dataset[:name => params[:field_name]]
    if @field
      @transformations = @field.transformations
      erb('transformations/for'.to_sym, :layout => false)
    else
      ''
    end
  end

  app.post "/projects/:project_id/resources/:resource_id/transformations/preview" do
    @resource = @project.resources_dataset[:id => params[:resource_id]]
    raise ResourceNotFound  unless @resource

    @transformation = Models::Transformation.new(params[:transformation])
    @preview = @resource.preview_transformation(@transformation)
    erb(:"transformations/preview", :layout => false)
  end
end