Module: Metajp::Shared::SuperCrud::Controller

Defined in:
lib/metajp/shared/super_crud/controller.rb

Instance Method Summary collapse

Instance Method Details

#_create(object, path = nil) ⇒ Object


CRUD helper methods




36
37
38
39
40
41
42
43
44
45
46
# File 'lib/metajp/shared/super_crud/controller.rb', line 36

def _create(object, path = nil)
  respond_to do |format|
    if object.save
      load_objects
      flash[:notice] = "#{@_name} was successfully created."
      format.html { redirect_to(set_path(object, path)) }
    else
      format.html { render :action => "new" }
    end
  end
end

#_destroy(name, object, object_name) ⇒ Object



59
60
61
62
63
64
# File 'lib/metajp/shared/super_crud/controller.rb', line 59

def _destroy(name, object, object_name)
  object.destroy
  respond_to do |format|
    format.js { simple_destroy(name, object, :message => "#{object_name} has been removed.") }
  end
end

#_update(object, path = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/metajp/shared/super_crud/controller.rb', line 48

def _update(object, path = nil)
  respond_to do |format|
    if object.update_attributes(@_attributes)
      flash[:notice] = "#{@_name} was successfully updated."
      format.html { redirect_to(set_path(object, path)) }
    else
      format.html { render :action => "edit" }
    end
  end
end

#add_filter_param(name) ⇒ Object

This is used to add extra parameters to the column_heading and pagination filtering

in the controller if you wanted to add the params[:search] to the filters
EG: add_extra_param(:search)


96
97
98
# File 'lib/metajp/shared/super_crud/controller.rb', line 96

def add_filter_param(name)
  @extra_params[name] = params[name] unless params[name].blank?
end

#add_search_if_present(collections) ⇒ Object

if the user has searched, add the search filter parameter this will pop the box in the top of the table listing



83
84
85
86
87
88
89
90
# File 'lib/metajp/shared/super_crud/controller.rb', line 83

def add_search_if_present(collections)
  if params[:search]
    @filter_text = "Search for '#{params[:search]}'"
    collections.search(params[:search])
  else
    collections
  end
end

#display_listObject


controller functionality – you can override or extend if you want




11
12
13
14
15
# File 'lib/metajp/shared/super_crud/controller.rb', line 11

def display_list    
  load_params
  load_objects
  update_list
end

#editObject



22
23
24
25
26
# File 'lib/metajp/shared/super_crud/controller.rb', line 22

def edit
  respond_to do |format|
    format.html # edit.html
  end
end

#get_order(ordering, exact = false) ⇒ Object


searching and sorting helpers




104
105
106
107
108
109
110
111
112
113
# File 'lib/metajp/shared/super_crud/controller.rb', line 104

def get_order(ordering, exact = false)
  if ordering
    order_array = ordering.split("_")
    direction = (order_array[0] == 'ascend') ? 'ASC' : 'DESC'
    column = ''
    order_array.each_with_index {|a, index| column += "#{a}_" if index > 1}
    column = column[0..-2]
    "#{column} #{direction}"
  end
end

#indexObject



17
18
19
20
# File 'lib/metajp/shared/super_crud/controller.rb', line 17

def index
  load_params
  load_objects
end

#is_admin?Boolean

determines if the user is an admin

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/metajp/shared/super_crud/controller.rb', line 71

def is_admin?
  flash[:notice] = 'You do not have access to this area.'
  redirect_to '/' unless current_user.admin?
end

#set_path(object, path) ⇒ Object

used to set the path in the #_update and #_create methods



77
78
79
# File 'lib/metajp/shared/super_crud/controller.rb', line 77

def set_path(object, path)
  path.blank? ? "#{@_path}/#{object.id}" : path
end

#showObject



28
29
30
# File 'lib/metajp/shared/super_crud/controller.rb', line 28

def show
  load_object
end

#simple_destroy(id, object, options = {}) ⇒ Object



133
134
135
136
137
138
# File 'lib/metajp/shared/super_crud/controller.rb', line 133

def simple_destroy(id, object, options = {})
  dom_id = options[:id] ? options[:id] : "#{id}-#{object.id}"
  render :update do |page|
    page.remove dom_id
  end
end

#update_list(options = {}) ⇒ Object


shared rjs methods




119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/metajp/shared/super_crud/controller.rb', line 119

def update_list(options = {})      
  options[:sidebar] = 'side_menu'    
  render :update do |page|
    page.replace_html 'list', :partial => 'list'
    page.replace_html 'side-menu', :partial => options[:sidebar] if options[:reload_sidebar]    
    page.replace_html options[:reload_partial], :partial => options[:reload_partial] if options[:reload_partial] 
    # show the advanced filter text
    unless @filter_text.blank?
      page.replace_html 'filter', filter_results 
      page.show "filter"
    end
  end
end