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



28
29
30
31
32
33
# File 'app/controllers/carnival/base_admin_controller.rb', line 28

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

#indexObject



8
9
10
11
12
13
14
15
16
17
18
# File 'app/controllers/carnival/base_admin_controller.rb', line 8

def index
  @query_form = QueryFormCreator.create(@presenter, params)
  @model = @presenter.model_class
  @query_service = QueryService.new(table_items, @presenter, @query_form)
  @thead_renderer = TheadRenderer.new @presenter.fields_for_action(:index), @query_form.sort_column, @query_form.sort_direction

  respond_to do |format|
    format.html { index_for_html }
    format.csv { index_for_csv }
  end
end

#load_dependent_select_optionsObject



48
49
50
51
52
53
# File 'app/controllers/carnival/base_admin_controller.rb', line 48

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



55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/carnival/base_admin_controller.rb', line 55

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

#render_inner_formObject



42
43
44
45
46
# File 'app/controllers/carnival/base_admin_controller.rb', line 42

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

#updateObject



35
36
37
38
39
40
# File 'app/controllers/carnival/base_admin_controller.rb', line 35

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