Skip to content

Commit

Permalink
Merge pull request #24 from sanger/devel
Browse files Browse the repository at this point in the history
Devel - Fixes PMB-2, PMB-3, PMB-4
  • Loading branch information
stevieing authored Nov 18, 2016
2 parents a98680c + 5245362 commit 6cf8032
Show file tree
Hide file tree
Showing 21 changed files with 502 additions and 146 deletions.
2 changes: 1 addition & 1 deletion rails/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ group :development, :test do
gem 'byebug'
end

gem 'active_model_serializers', '~> 0.10.0.rc4'
gem 'active_model_serializers', '~> 0.10'

gem 'puma'

Expand Down
6 changes: 3 additions & 3 deletions rails/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ GEM
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
active_model_serializers (0.10.0.rc4)
active_model_serializers (0.10.0)
actionpack (>= 4.0)
activemodel (>= 4.0)
railties (>= 4.0)
Expand Down Expand Up @@ -156,7 +156,7 @@ PLATFORMS
ruby

DEPENDENCIES
active_model_serializers (~> 0.10.0.rc4)
active_model_serializers (~> 0.10)
byebug
exception_notification
factory_girl_rails
Expand All @@ -170,4 +170,4 @@ DEPENDENCIES
sqlite3

BUNDLED WITH
1.11.2
1.12.1
6 changes: 5 additions & 1 deletion rails/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ class ApplicationController < ActionController::API
def test_exception_notifier
raise 'This is a test. This is only a test.'
end


def render_error(resource, status: :unprocessable_entity)
render json: resource, status: status, serializer: ActiveModel::Serializer::ErrorSerializer
end

end
14 changes: 9 additions & 5 deletions rails/app/controllers/v1/label_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class V1::LabelTemplatesController < ApplicationController
#'**' includes all nested associated resources in the "included" member

def index
render json: LabelTemplate.all, include: '**'
render json: LabelTemplate.filter(filter_parameters[:filter]), include: '**'
end

def show
Expand All @@ -13,9 +13,9 @@ def show
def create
label_template = LabelTemplate.new label_template_params
if label_template.save
render json: label_template, include: '**'
render json: label_template, include: '**', status: :created
else
render json: { errors: label_template.errors }, status: :unprocessable_entity
render_error label_template
end
end

Expand All @@ -24,7 +24,7 @@ def update
if label_template.update_attributes(label_template_params)
render json: label_template, include: '**'
else
render json: { errors: label_template.errors }, status: :unprocessable_entity
render_error label_template
end
end

Expand All @@ -37,5 +37,9 @@ def current_resource
def label_template_params
params.require(:data).require(:attributes).permit(LabelTemplate.permitted_attributes)
end


def filter_parameters
params.permit(filter: [:name])
end

end
14 changes: 9 additions & 5 deletions rails/app/controllers/v1/label_types_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class V1::LabelTypesController < ApplicationController

def index
render json: LabelType.all
render json: LabelType.filter(filter_params[:filter])
end

def show
Expand All @@ -11,9 +11,9 @@ def show
def create
label_type = LabelType.new label_type_params
if label_type.save
render json: label_type
render json: label_type, status: :created
else
render json: { errors: label_type.errors }, status: :unprocessable_entity
render_error label_type
end
end

Expand All @@ -22,7 +22,7 @@ def update
if label_type.update_attributes(label_type_params)
render json: label_type
else
render json: { errors: label_type.errors }, status: :unprocessable_entity
render_error label_type
end
end

Expand All @@ -35,5 +35,9 @@ def current_resource
def label_type_params
params.require(:data).require(:attributes).permit(:name, :feed_value, :fine_adjustment,:pitch_length, :print_width,:print_length)
end


def filter_params
params.permit(filter: [:name])
end

end
14 changes: 11 additions & 3 deletions rails/app/controllers/v1/print_jobs_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
class V1::PrintJobsController < ApplicationController

def create
print_job = LabelPrinter::PrintJob.build(params["data"]["attributes"])
print_job = LabelPrinter::PrintJob.build(print_job_params)
if print_job.execute
render json: print_job, serializer: PrintJobSerializer
render json: print_job, serializer: PrintJobSerializer, status: :created
else
render json: { errors: print_job.errors }, status: :unprocessable_entity
render_error print_job
end
end

private

def print_job_params
permitted_params = params.require(:data).require(:attributes).permit(:printer_name, :label_template_id).tap do |whitelisted|
whitelisted[:labels] = params[:data][:attributes][:labels]
end
end

Expand Down
14 changes: 9 additions & 5 deletions rails/app/controllers/v1/printers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class V1::PrintersController < ApplicationController

def index
render json: Printer.all
render json: Printer.filter(filter_params[:filter])
end

def show
Expand All @@ -11,9 +11,9 @@ def show
def create
printer = Printer.new(printer_params)
if printer.save
render json: printer
render json: printer, status: :created
else
render json: { errors: printer.errors }, status: :unprocessable_entity
render_error printer
end
end

Expand All @@ -24,7 +24,11 @@ def current_resource
end

def printer_params
params.require(:data).require(:attributes).permit(:name)
params.require(:data).require(:attributes).permit(:name, :protocol)
end


def filter_params
params.permit(filter: [:name, :protocol])
end

end
6 changes: 3 additions & 3 deletions rails/app/label_printer/label_printer/print_job/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ def input
private

def check_printer
errors.add(:printer, "Printer does not exist") unless printer
errors.add(:printer, "does not exist") unless printer
end

def check_label_template
errors.add(:label_template, "Label template does not exist") unless label_template
errors.add(:label_template, "does not exist") unless label_template
end

def check_labels
errors.add(:labels, "There should be some labels") unless labels.any?
errors.add(:labels, "can't be empty") unless labels.any?
end

end
Expand Down
23 changes: 23 additions & 0 deletions rails/app/models/concerns/filterable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Filterable

extend ActiveSupport::Concern

class_methods do

def before_filter(&block)
@_before_filter = block
end

def filter(filters)
# If nil is passed in
filters ||= {}

# Run the filters through the before_filter
filters = @_before_filter.call(filters) if @_before_filter

where(filters)
end

end

end
6 changes: 4 additions & 2 deletions rails/app/models/label_template.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class LabelTemplate < ActiveRecord::Base

include Filterable

belongs_to :label_type
has_many :labels, dependent: :destroy

Expand Down Expand Up @@ -29,10 +31,10 @@ def label_fields
##
# For use as permitted attributes in the controller
def self.permitted_attributes
[
[
"name",
"label_type_id",
"labels_attributes" => Label.permitted_attributes,
"labels_attributes" => Label.permitted_attributes,
]
end

Expand Down
4 changes: 3 additions & 1 deletion rails/app/models/label_type.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Example:
# Example:
# LabelType.create(name: "Plate", feed_value: "004", fine_adjustment: "08", pitch_length: "0110", print_width: "0920", print_length: "0080")
# Each label type will have different size and positioning.
# A label template must have a label type
class LabelType < ActiveRecord::Base

include Filterable

validates :name, presence: true, uniqueness: {case_sensitive: false}

validates :pitch_length, :print_width, :print_length, presence: true, format: {with: /\A\d{4}\z/}
Expand Down
8 changes: 7 additions & 1 deletion rails/app/models/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
# The TOF protocol implements the ability to send the print job to a file.
class Printer < ActiveRecord::Base

include Filterable

validates :name, presence: true, uniqueness: {case_sensitive: false}

enum protocol: [:LPD, :IPP, :TOF]

end
before_filter do |filters|
filters.has_key?(:protocol) ? filters.merge!(protocol: protocols[filters[:protocol]]) : filters
end

end
Loading

0 comments on commit 6cf8032

Please sign in to comment.