Module: Mokio::Concerns::Controllers::Common

Extended by:
ActiveSupport::Concern
Includes:
Mokio::Concerns::Common::Translations
Included in:
Mokio::CommonController
Defined in:
lib/mokio/concerns/controllers/common.rb

Overview

Concern for CommonController. Many important controller logic is placed here.

Most of Mokio’s controllers are inherited by this controller. The main logic is to not repeat same part of code with changing only variables. Model and instance variable name is parsed from controller name (see Mokio::Concerns::Common::ControllerObject). Its also using translations specified by us to match action name (model.deleted, model.created, model.not_updated etc.). Devise’s load_and_authorize_resource is also placed here

CommonController provides:

  • index method with json sending data.

Its using common/index view and place date into jquery.datatable plugin (you can see in backend/datatable.js.coffee.erb file). We send json as CommonsDatatable object (app/datatables/commons_datatable.rb) Columns displayed in table are specified in used model inside method called columns_for_table.

  • new method with building meta/gmap if they are enabled in used model

Its using common/new view and need partial ‘_form’ with specifed form elements. Main form uses simple_form.

  • edit method with building gmap if its enabled in used model

Its using common/edit and need same partial as in ‘new’ action.

  • create method

Simple creating and saving object to database if passed validation.

  • update method

Simple updating object in database

  • destroy method

Standard destroying object from database. Redirects back (see in Mokio::Concerns::Controllers::Base)

  • copy method

Works similarly to ‘new’ but fill new object data by copying record using ‘amoeba’ gem

  • update_active method (ajax)

Ajax updating active field

Instance Method Summary collapse

Instance Method Details

#additional_action_buttonsObject

Returns string with path to partial json Override when you want to add other action buttons to datatable row in index view

Examples

"mokio/my_objects/some_row_action"

refers to file:

"mokio/my_objects/_some_row_action.json.*(slim/erb/haml)"


242
243
244
# File 'lib/mokio/concerns/controllers/common.rb', line 242

def additional_action_buttons
  nil
end

#additional_index_buttonsObject

Returns string with path to partial html Override when you want to add other buttons to header in index view

Examples

"mokio/my_objects/some_btn"

refers to file:

"mokio/my_objects/_some_btn.html.*(slim/erb/haml)"


229
230
231
# File 'lib/mokio/concerns/controllers/common.rb', line 229

def additional_index_buttons
  nil
end

#ajax_collectionObject



59
60
61
# File 'lib/mokio/concerns/controllers/common.rb', line 59

def ajax_collection
  render json: []
end

#copyObject

Similar to new action but creates new object with copied data. This method is using amoeba_dup to duplicate object with every associations specified in Model (gem amoeba)

Examples

class Post < ActiveRecord::Base
  has_many :comments
  has_many :tags
  has_many :authors

amoeba do
  include_association [:tags, :authors]
end

If you use this method add to your controller resources in routes.rb:

member do
  get :copy
end



161
162
163
164
165
166
167
# File 'lib/mokio/concerns/controllers/common.rb', line 161

def copy
  create_obj(obj.amoeba_dup)
  build_enabled(obj)

  #add (copy) suffix to name/title, etc.
  obj.send(obj.class.columns_for_table[0] + '=',  obj.send(obj.class.columns_for_table[0]) + I18n.t('backend.copy_suffix'))
end

#createObject

Standard create action. Create and save object for specified class in database



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mokio/concerns/controllers/common.rb', line 91

def create
  create_obj( @obj_class.new(obj_params) )
  respond_to do |format|
    if obj.save
      if !params[:save_and_new].blank?
        format.html { redirect_to obj_new_url(@obj_class.new), notice: CommonTranslation.created(obj) }
        format.json { render action: 'new', status: :created, location: obj }
      else
        format.html { redirect_to obj_index_url, notice: CommonTranslation.created(obj) }
        format.json { render action: 'index', status: :created, location: obj }
      end
    else
      format.html { render "new", notice: CommonTranslation.not_created(obj) }
      format.json { render json: @obj.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

Standard destroy action. Remove object for specified class from database



133
134
135
136
137
138
139
140
141
# File 'lib/mokio/concerns/controllers/common.rb', line 133

def destroy
  respond_to do |format|
    if obj.destroy
      redirect_back(format, obj_index_url, CommonTranslation.deleted(obj))
    else
      redirect_back(format, obj_index_url, CommonTranslation.not_deleted(obj))
    end
  end
end

#editObject

Standard edit action.



84
85
86
# File 'lib/mokio/concerns/controllers/common.rb', line 84

def edit
  obj.build_gmap if obj.class.has_gmap_enabled? && obj.gmap.nil? # build gmap if it hasn't created before. Relation with gmap isn't nessesary !
end

#indexObject

Index action renders json with parameters to jquery.datatables



65
66
67
68
69
70
71
# File 'lib/mokio/concerns/controllers/common.rb', line 65

def index
  respond_to do |format|
    # @TODO zrobic cos by przy formacie html nie renderował się javascript z datatable
    format.html # { create_obj(@obj_class.page( params[:page] ) ) }
    format.json { render json: ::CommonsDatatable.new(view_context, @obj_class) }
  end
end

#newObject

Standard new action. Create object for specified class



76
77
78
79
# File 'lib/mokio/concerns/controllers/common.rb', line 76

def new
  create_obj(@obj_class.new)
  build_enabled(obj)
end

#render_additional_action_buttons(obj) ⇒ Object

Renders additional action buttons in datatable row in index view

Attributes

  • obj - record object from database



210
211
212
213
214
215
216
217
218
# File 'lib/mokio/concerns/controllers/common.rb', line 210

def render_additional_action_buttons(obj)
  template = self.additional_action_buttons
  if !template.nil?
    result = render_to_string  :partial => template, :locals => {obj: obj}
    result.html_safe
  else
    ""
  end
end

#render_additional_index_buttonsObject

Renders additional buttons in index view



195
196
197
198
199
200
201
202
203
# File 'lib/mokio/concerns/controllers/common.rb', line 195

def render_additional_index_buttons
  template = self.additional_index_buttons
  if !template.nil?
      result = render_to_string :partial => template
    result.html_safe
  else
    ""
  end
end

#updateObject

Standard update action. Update params object for specified class in database



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mokio/concerns/controllers/common.rb', line 112

def update

  respond_to do |format|
    if obj.update(obj_params)
      if !params[:save_and_new].blank?
        format.html { redirect_to obj_new_url(@obj_class.new), notice: CommonTranslation.updated(obj) }
        format.json { head :no_content }
      else
        format.html { redirect_to obj_index_url, notice: CommonTranslation.updated(obj) }
        format.json { render action: 'index', status: :created, location: obj }
      end
    else
      format.html { render "edit", notice: CommonTranslation.not_updated(obj) }
      format.json { render json: @obj.errors, status: :unprocessable_entity }
    end
  end
end

#update_activeObject

Method for ajax updating “active” attribute If you use this method, add to your controller resources in routes.rb:

member do
  get :update_active
end


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/mokio/concerns/controllers/common.rb', line 175

def update_active
  if obj.update_attribute(:active, !obj.active)
    if obj.active
      flash[:notice] = CommonTranslation.update_active_true(obj)
    else
      flash[:error] = CommonTranslation.update_active_false(obj)
    end
  else
    logger.error "[#{@obj_class}] id:#{obj.id} name/title:#{obj_title} cannot update_attribute active"
  end

  respond_to do |format|
    format.html { head :ok }
    format.js { head :ok }
  end
end