Class: Crm::SiteMapSearchForm

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model
Defined in:
app/forms/crm/site_map_search_form.rb

Overview

Form object for SiteMap search that combines Ransack predicates with custom UI controls
and action parameters in a single, cohesive object.

Usage in controller:
@search_form = Crm::SiteMapSearchForm.new(params)
@site_maps = @search_form.results

Usage in view:
simple_form_for @search_form, url: ..., method: :get do |f|
f.input :locale_eq, ...
f.input :url_predicate, ...
f.input :auto_purge_action, ...
end

Supports full URL input - when a full URL is pasted (e.g., https://www.warmlyyours.com/en-US/floor-heating),
the form automatically extracts the locale and path for searching.

Constant Summary collapse

URL_PREDICATE_OPTIONS =

Path predicate options for the filter dropdown

[
  ['Path Contains', 'path_cont'],
  ['Path Starts With', 'path_start'],
  ['Path Ends With', 'path_end'],
  ['Path Equals', 'path_eq']
].freeze
ALLOWED_URL_PREDICATES =

Allowed url predicates.

URL_PREDICATE_OPTIONS.map(&:second).freeze
STATUS_FILTER_OPTIONS =

Available status filter options.

[
  ['All', ''],
  ['Not 200 (Failed)', 'not_200'],
  ['200 (OK)', '200'],
  ['301 (Redirect)', '301'],
  ['404 (Not Found)', '404'],
  ['500 (Server Error)', '500']
].freeze
AUTO_PURGE_OPTIONS =

Available auto purge options.

[
  ['None', 'none'],
  ['Page (URLs)', 'page'],
  ['Tag (Categories)', 'tag']
].freeze
VALID_HOSTS =

Valid hosts for URL parsing

%w[
  www.warmlyyours.com
  www.warmlyyours.ca
  www.warmlyyours.me
].freeze
OPEN_REC_OPTIONS =

Available open rec options.

[
  ['All', ''],
  ['Has Open Issues', 'yes'],
  ['No Open Issues', 'no']
].freeze
ANALYSIS_FRESHNESS_OPTIONS =

Available analysis freshness options.

[
  ['All', ''],
  ['Stale (recrawled after analysis)', 'stale'],
  ['Fresh', 'fresh'],
  ['No analysis', 'none']
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ SiteMapSearchForm

Builds the form from controller params, extracting locale and path when
url_value is a full URL on a valid host.

Parameters:

  • params (ActionController::Parameters, Hash) (defaults to: {})

    request params; search
    fields are read from the nested q hash



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/forms/crm/site_map_search_form.rb', line 95

def initialize(params = {})
  # Handle both ActionController::Parameters and plain Hash
  q_params = params[:q] || params['q'] || {}
  q_params = q_params.to_unsafe_h if q_params.respond_to?(:to_unsafe_h)

  raw_url_value = q_params['url_value']
  parsed = parse_url_value(raw_url_value)

  super(
    locale_eq: parsed[:locale] || q_params['locale_eq'],
    category_eq: q_params['category_eq'],
    last_status_eq: q_params['last_status_eq'],
    url_predicate: q_params['url_predicate'].presence || 'path_cont',
    url_value: parsed[:path] || raw_url_value,
    keyword: q_params['keyword'],
    has_open_recommendations: q_params['has_open_recommendations'],
    seo_score_gteq: q_params['seo_score_gteq'],
    seo_score_lteq: q_params['seo_score_lteq'],
    analysis_freshness: q_params['analysis_freshness'],
    s: q_params['s'],
    auto_purge_action: q_params['auto_purge_action'].presence || 'none'
  )
end

Instance Method Details

#auto_purge?Boolean

Checks if an auto purge action is requested.

Returns:

  • (Boolean)

    true when auto_purge_action is set to something other than 'none'



181
182
183
# File 'app/forms/crm/site_map_search_form.rb', line 181

def auto_purge?
  auto_purge_action.present? && auto_purge_action != 'none'
end

#model_nameActiveModel::Name

For SimpleForm compatibility - used to determine form field names.

Returns:

  • (ActiveModel::Name)

    model name scoped as q so params nest under q



160
161
162
# File 'app/forms/crm/site_map_search_form.rb', line 160

def model_name
  ActiveModel::Name.new(self.class, nil, 'q')
end

#persisted?false

Whether the form object is persisted (always false for SimpleForm).

Returns:

  • (false)


174
175
176
# File 'app/forms/crm/site_map_search_form.rb', line 174

def persisted?
  false
end

#purge_by_page?Boolean

Whether the requested purge targets pages (URLs).

Returns:

  • (Boolean)


188
189
190
# File 'app/forms/crm/site_map_search_form.rb', line 188

def purge_by_page?
  auto_purge_action == 'page'
end

#purge_by_tag?Boolean

Whether the requested purge targets tags (categories).

Returns:

  • (Boolean)


195
196
197
# File 'app/forms/crm/site_map_search_form.rb', line 195

def purge_by_tag?
  auto_purge_action == 'tag'
end

#ransack_searchRansack::Search

Builds the Ransack search object with normalized parameters.

Returns:

  • (Ransack::Search)

    memoized Ransack search over SiteMap



122
123
124
# File 'app/forms/crm/site_map_search_form.rb', line 122

def ransack_search
  @ransack_search ||= SiteMap.ransack(ransack_params)
end

#resultsActiveRecord::Relation<SiteMap>

Delegates to ransack for results, with optional keyword and recommendation filtering.

Returns:

  • (ActiveRecord::Relation<SiteMap>)

    matching site map pages



144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/forms/crm/site_map_search_form.rb', line 144

def results
  scope = ransack_search.result

  if keyword.present?
    scope = scope.joins(:seo_page_keywords)
                 .where(seo_page_keywords: { keyword: keyword })
                 .distinct
  end

  scope = apply_recommendation_filter(scope)
  apply_analysis_freshness_filter(scope)
end

#to_keynil

For SimpleForm compatibility.

Returns:

  • (nil)

    always nil; the form object is never persisted



167
168
169
# File 'app/forms/crm/site_map_search_form.rb', line 167

def to_key
  nil
end