Module: Discerner::Methods::Controllers::SearchesController

Included in:
SearchesController
Defined in:
lib/discerner/methods/controllers/searches_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'lib/discerner/methods/controllers/searches_controller.rb', line 5

def self.included(base)
  base.send :before_filter, :load_search, only: [:edit, :update, :rename, :destroy, :show]
end

Instance Method Details

#createObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/discerner/methods/controllers/searches_controller.rb', line 19

def create
  @discerner_search           = Discerner::Search.new(search_params)
  @discerner_search.username  = discerner_user.username unless discerner_user.blank?

  set_searchable_dictionaries
  set_searchables
  respond_to do |format|
    if @discerner_search.save
      format.html { redirect_to(edit_search_path(@discerner_search)) }
    else
      format.html { render action: "new" }
    end
  end
end

#destroyObject



89
90
91
92
93
94
95
# File 'lib/discerner/methods/controllers/searches_controller.rb', line 89

def destroy
  @discerner_search.deleted_at = Time.now
  @discerner_search.save
  respond_to do |format|
    format.html { redirect_to searches_path }
  end
end

#editObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/discerner/methods/controllers/searches_controller.rb', line 34

def edit
  set_searchable_dictionaries
  set_searchables

  if @discerner_search.disabled?
    error_message = "There is an issue with the this search that has to be corrected before it can be executed"
    if @discerner_search.warnings.any?
      error_message << ': '
      error_message << @discerner_search.warnings.full_messages.join(',')
    end
  else
    if dictionary_model
      dictionary =  dictionary_model.new(@discerner_search)
      if dictionary.respond_to?('search')
        @results = dictionary.search(params, dictionary_search_options)
        @discerner_search.last_executed = Time.now
        @discerner_search.save!
      else
        error_message = "Model '#{dictionary_model_name}' instance does not respond to 'search' method. You need to implement it to be able to run search on this dictionary"
      end
    else
      error_message = "Model '#{dictionary_model_name}' could not be found. You need to create it to be able to run search on this dictionary"
    end
  end
  flash[:error] = error_message unless error_message.blank?
end

#indexObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/discerner/methods/controllers/searches_controller.rb', line 75

def index
  searches = Discerner::Search.not_deleted.includes(
    :dictionary,
    :export_parameters   => [parameter: [:parameter_type]],
    search_combinations: [combined_search: [search_parameters: [parameter: [:parameter_type], search_parameter_values: [:parameter_value]]]],
    :search_parameters   => [parameter: [:parameter_type], search_parameter_values: [:parameter_value]])

  username = discerner_user.username unless discerner_user.blank?
  searches = searches.by_user(username) unless username.blank?

  searches = searches.where('discerner_searches.name like ?', '%' + params[:query] + '%') unless params[:query].blank?
  @discerner_searches = searches.order("discerner_searches.last_executed DESC")
end

#newObject



9
10
11
12
13
14
15
16
17
# File 'lib/discerner/methods/controllers/searches_controller.rb', line 9

def new
  set_searchable_dictionaries
  if @searchable_dictionaries.any?
    set_searchables
    @discerner_search = Discerner::Search.new
  else
    flash[:error] = 'No searchable dictionaries found. Make sure that dictionaries are loaded.'
  end
end

#showObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/discerner/methods/controllers/searches_controller.rb', line 97

def show
  if @discerner_search.disabled?
    error_message = "There is an issue with the this search that has to be corrected before it can be exported"
    if @discerner_search.warnings.any?
      error_message << ': '
      error_message << @discerner_search.warnings.full_messages.join(',')
    end
  else
    if dictionary_model
      dictionary =  dictionary_model.new(@discerner_search)
      if not dictionary.respond_to?('export')
        error_message = "Model '#{dictionary_model_name}' instance does not respond to 'export' method. You need to implement it to be able to run export on this dictionary"
      end
    else
      error_message = "Model '#{dictionary_model_name}' could not be found. You need to create it to be able to run export on this dictionary"
    end
  end
  flash[:error] = error_message unless error_message.blank?

  respond_to do |format|
    if error_message
      format.html
      format.csv { redirect_to export_parameters_path(@discerner_search)  }
      format.xls { redirect_to export_parameters_path(@discerner_search)  }
    else
      @export_data = dictionary.export(params, dictionary_search_options)
      filename ="#{@discerner_search.parameterized_name}_#{Date.today.strftime('%m_%d_%Y')}"
      format.html
      format.csv do

        send_data @export_data,
          type: 'text/csv; charset=iso-8859-1; header=present',
          disposition: "attachment; filename=#{filename}.csv"
      end
      format.xls do
        headers["Content-type"] = "application/vnd.ms-excel"
        headers['Content-Transfer-Encoding'] = 'binary'
        headers['Expires'] = '0'
        headers['Pragma'] = 'public'
        headers["Cache-Control"] = "must-revalidate, post-check=0, pre-check=0"
        headers["Content-Disposition"] = "attachment; filename=\"#{filename}.xls\""
        headers['Content-Description'] = 'File Transfer'
        render "discerner/dictionaries/#{@discerner_search.dictionary.parameterized_name}/show"
      end
    end
  end
end

#updateObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/discerner/methods/controllers/searches_controller.rb', line 61

def update
  set_searchable_dictionaries
  set_searchables
  respond_to do |format|
    if @discerner_search.update_attributes(search_params)
      format.html { redirect_to(edit_search_path(@discerner_search), notice: 'Search was successfully updated.') }
      format.js
    else
      format.html { render action: "edit" }
      format.js
    end
  end
end