Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds service action to ReportFiltersController#create to ensure dates #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions app/controllers/clark_kent/report_filters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ def new
end

def create
report_filter_class = @filterable.get_filter_class(report_filter_params)
@report_filter = report_filter_class.new(report_filter_params)
@report_filter.save
render partial: 'show_wrapper', locals: {report_filter: @report_filter}
service = ClarkKent::ReportFiltersController::CreateReportFilter.new(@filterable, report_filter_params)
service.create
if service.errors.any?
report_filter = ClarkKent::ReportFilter.new(filterable_id: @filterable.id, filterable_type: @filterable.class.name)
render json: {
html: render_to_string(partial: 'form', locals: {report_filter: service.report_filter}),
flash_message: service.errors.join(', ')
}, status: :conflict
else
render partial: 'show_wrapper', locals: {report_filter: service.report_filter}
end
end

def show
Expand Down Expand Up @@ -39,20 +46,22 @@ def prepare_report_filter
end

def prepare_report
@filterable_id = params[:filterable_id]
@filterable_type = params[:filterable_type]
@filterable_id ||= params[:report_filter][:filterable_id] if params[:report_filter]
@filterable_type ||= params[:report_filter][:filterable_type] if params[:report_filter]
@filterable_class = @filterable_type.constantize if @filterable_type
@filterable = @filterable_class.find(@filterable_id) if @filterable_id and @filterable_class
@filterable ||= @report_filter.filterable if @report_filter
@filterable_id = params[:filterable_id]
@filterable_type = params[:filterable_type]
@filterable_id ||= params[:report_filter][:filterable_id] if params[:report_filter]
@filterable_type ||= params[:report_filter][:filterable_type] if params[:report_filter]
@filterable_id ||= params[:report_date_filter][:filterable_id] if params[:report_date_filter]
@filterable_type ||= params[:report_date_filter][:filterable_type] if params[:report_date_filter]
@filterable_class = @filterable_type.constantize if @filterable_type
@filterable = @filterable_class.find(@filterable_id) if @filterable_id and @filterable_class
@filterable ||= @report_filter.filterable if @report_filter
end

def report_filter_params
if @report_filter
these_params = params[@report_filter.class.name.underscore.gsub(/.*\//,'')]
else
these_params = params[:report_filter]
these_params = params[:report_filter] || params[:report_date_filter]
end
these_params.permit(:filterable_id, :filterable_type, :string, :filter_name, :filter_value, :type, :duration, :kind_of_day, :offset)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class ClarkKent::ReportFiltersController::CreateReportFilter
attr_accessor :filterable, :report_filter_params, :errors, :report_filter
def initialize(filterable, report_filter_params)
@filterable = filterable
@report_filter_params = report_filter_params
@errors = []
end

def create
fetch_report_filter_class
build_report_filter
validate_report_filter or return false
@report_filter.save
end

def build_report_filter
@report_filter = @report_filter_class.new(report_filter_params)
end

def fetch_report_filter_class
@report_filter_class = @filterable.get_filter_class(report_filter_params)
end

def validate_report_filter
if @report_filter_class.to_s == "ClarkKent::ReportDateFilter"
if !@report_filter.duration.present? ||
!@report_filter.kind_of_day.present? ||
!@report_filter.offset.present?
errors << "You must specify all filter timing parameters"
return false
end
end
return true
end
end
1 change: 1 addition & 0 deletions app/views/clark_kent/report_filters/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ajax_form: true,
sub_type: "AddALineForm",
ajax_target: report_filter.new_record? ? "#report_filter_list_#{dom_id(report_filter.filterable)}" : "#report_filter_#{report_filter.id}",
error_target: "#new_report_filter_container_#{dom_id(report_filter.filterable)}",
insert_method: report_filter.new_record? ? 'append' : 'html',
container: dom_target(report_filter.filterable, :new_report_filter_container),
ajax_flash: true } } do |f| %>
Expand Down