Class: Carnival::BaseAdminController

Inherits:
InheritedResources::Base
  • Object
show all
Defined in:
app/controllers/carnival/base_admin_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



69
70
71
72
73
74
# File 'app/controllers/carnival/base_admin_controller.rb', line 69

def create
  create! do |success, failure|
    success.html { redirect_to back_or_model_path, :notice => I18n.t("messages.created") }
    failure.html { instantiate_model and render 'new' }
  end
end

#editObject



63
64
65
66
67
# File 'app/controllers/carnival/base_admin_controller.rb', line 63

def edit
  edit! do
    instantiate_model
  end
end

#homeObject



8
9
# File 'app/controllers/carnival/base_admin_controller.rb', line 8

def home
end

#indexObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/carnival/base_admin_controller.rb', line 27

def index
  @query_form = Carnival::QueryFormCreator.create(@presenter, params)
  @model = @presenter.model_class
  @query_service = Carnival::QueryService.new(table_items || @model, @presenter, @query_form)

  respond_to do |format|
    format.html do
      @records = @query_service.get_query
      last_page = (@query_service.total_records / @presenter.items_per_page.to_f).ceil
      @paginator = Carnival::Paginator.new @query_form.page, last_page
      @thead_renderer = Carnival::TheadRenderer.new @presenter.fields_for_action(:index), @query_form.sort_column, @query_form.sort_direction
    end
    format.csv do
      @records = @query_service.records_without_pagination
      render :csv => @model.model_name.human
    end
    format.pdf do
      @records = @query_service.records_without_pagination
      @thead_renderer = Carnival::TheadRenderer.new @presenter.fields_for_action(:index), @query_form.sort_column, @query_form.sort_direction
      render :pdf => t("activerecord.attributes.#{@presenter.full_model_name}.pdf_name") , :template => 'carnival/base_admin/index.pdf.haml',  :show_as_html => params[:debug].present?
    end
  end
end

#load_dependent_select_optionsObject



83
84
85
86
87
88
# File 'app/controllers/carnival/base_admin_controller.rb', line 83

def load_dependent_select_options
  presenter = params[:presenter].constantize.send(:new, :controller => self)
  model = presenter.relation_model(params[:field].gsub("_id", "").to_sym)
  @options = model.list_for_select(add_empty_option: true, query: ["#{params[:dependency_field]} = ?", params[:dependency_value]])
  render layout: nil
end

#load_select_optionsObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/carnival/base_admin_controller.rb', line 90

def load_select_options
  model_name = params[:model_name]
  search_field = params[:search_field]
  presenter = params[:presenter_name].constantize.send(:new, :controller => self)
  model = presenter.relation_model(model_name.to_sym)
  list = []
  model.where("#{search_field} like '%#{params[:q]}%'").each do |elem|
    list << {id: elem.id, text: elem.send(search_field.to_sym)}
  end

  render :json => list
end

#newObject



57
58
59
60
61
# File 'app/controllers/carnival/base_admin_controller.rb', line 57

def new
  new! do
    instantiate_model
  end
end

#presenter_name(field) ⇒ Object



21
22
23
24
25
# File 'app/controllers/carnival/base_admin_controller.rb', line 21

def presenter_name field
  field_name =  field.split('/').last
  carnival_mount = Carnival::Config.mount_at
  "#{carnival_mount}/#{field_name.singularize}_presenter".classify.constantize
end

#render_inner_formObject



15
16
17
18
19
# File 'app/controllers/carnival/base_admin_controller.rb', line 15

def render_inner_form
  @presenter = presenter_name(params[:field]).new controller: self
  model_class = params[:field].classify.constantize
  @model_object = model_class.find(params[:id])
end

#showObject



51
52
53
54
55
# File 'app/controllers/carnival/base_admin_controller.rb', line 51

def show
  show! do
    instantiate_model
  end
end

#table_itemsObject



11
12
13
# File 'app/controllers/carnival/base_admin_controller.rb', line 11

def table_items
  nil
end

#updateObject



76
77
78
79
80
81
# File 'app/controllers/carnival/base_admin_controller.rb', line 76

def update
  update! do |success, failure|
    success.html { redirect_to back_or_model_path, :notice => I18n.t("messages.updated") }
    failure.html { instantiate_model and render 'edit' }
  end
end