Sha256: 51da49de014a9e06df5bf72a3b60e10d95c9067c95f2101db93083910763fefe

Contents?: true

Size: 1.73 KB

Versions: 7

Compression:

Stored size: 1.73 KB

Contents

require_dependency "front_end_builds/application_controller"

module FrontEndBuilds
  class AppsController < ApplicationController
    before_filter :set_app , :only => [:show, :destroy]
    respond_to :json

    def index
      apps = []
      builds = []

      FrontEndBuilds::App.all.each do |app|
        apps.push serialize_app(app)
        builds.concat serialize_builds(app)
      end

      respond_with({
        apps: apps,
        builds: builds
      })
    end

    def show
      respond_with({
        app: serialize_app(@app),
        builds: serialize_builds(@app)
      })
    end

    def create
      @app = FrontEndBuilds::App.new(app_create_params)

      if @app.save!
        hash = { app: @app }
        respond_with hash, location: nil

      else
        respond_with(
          {errors: @app.errors},
          status: :unprocessable_entity
        )
      end
    end

    def destroy
      if @app.destroy
        hash = { app: @app }
        respond_with hash, location: nil

      else
        respond_with(
          {errors: @app.errors},
          status: :unprocessable_entity
        )
      end
    end

    private

    def set_app    
      @app = FrontEndBuilds::App.find params[:id]
    end

    def app_create_params
      params.require(:app).permit(
        :name
      )
    end

    def serialize_app(app)
      build_ids = app.builds.map {|build| build.id}

      app.as_json.merge( {build_ids: build_ids} )
    end

    def serialize_builds(app)
      builds = app.builds.as_json
      best_build = app.find_best_build

      if best_build
        builds.each do |build|
          if build["id"] === best_build.id
            build["is_best"] = true
          end
        end
      end

      return builds
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
front_end_builds-0.0.13 app/controllers/front_end_builds/apps_controller.rb
front_end_builds-0.0.12 app/controllers/front_end_builds/apps_controller.rb
front_end_builds-0.0.11 app/controllers/front_end_builds/apps_controller.rb
front_end_builds-0.0.10 app/controllers/front_end_builds/apps_controller.rb
front_end_builds-0.0.9 app/controllers/front_end_builds/apps_controller.rb
front_end_builds-0.0.8 app/controllers/front_end_builds/apps_controller.rb
front_end_builds-0.0.7 app/controllers/front_end_builds/apps_controller.rb