Module: Tramway::RecordsHelper

Defined in:
app/helpers/tramway/records_helper.rb

Instance Method Summary collapse

Instance Method Details

#active_tab(tab, index) ⇒ Object



72
73
74
75
# File 'app/helpers/tramway/records_helper.rb', line 72

def active_tab(tab, index)
  return :active if params[:scope].nil? && index.zero?
  return :active if params[:search].nil? && params[:scope].to_s == tab.to_s
end

#build_options_for_select(name, collection) ⇒ Object



45
46
47
48
# File 'app/helpers/tramway/records_helper.rb', line 45

def build_options_for_select(name, collection)
  selected_value = params[:list_filters].present? ? params[:list_filters][name] : nil
  options_for_select(collection, selected_value)
end

#collection_human_name(model_name:, collection_name:) ⇒ Object



58
59
60
61
62
63
64
# File 'app/helpers/tramway/records_helper.rb', line 58

def collection_human_name(model_name:, collection_name:)
  if t("default.collections.#{collection_name}").include?('<span')
    t("collections.#{model_name}.#{collection_name}").pluralize(:ru)
  else
    t("default.collections.#{collection_name}")
  end
end

#current_model_record_path(*args, **options) ⇒ Object

FIXME: replace to module



5
6
7
# File 'app/helpers/tramway/records_helper.rb', line 5

def current_model_record_path(*args, **options)
  record_path args, options.merge(model: params[:model])
end

#current_model_records_path(*args, **options) ⇒ Object



17
18
19
# File 'app/helpers/tramway/records_helper.rb', line 17

def current_model_records_path(*args, **options)
  records_path args, options.merge(model: params[:model])
end

#decorator_class(model_name = nil) ⇒ Object



29
30
31
# File 'app/helpers/tramway/records_helper.rb', line 29

def decorator_class(model_name = nil)
  "#{model_name || model_class}Decorator".constantize
end

#edit_current_model_record_path(*args, **options) ⇒ Object



9
10
11
# File 'app/helpers/tramway/records_helper.rb', line 9

def edit_current_model_record_path(*args, **options)
  edit_record_path args, options.merge(model: params[:model])
end

#index_path_of_model(model_class, tab, filter) ⇒ Object



50
51
52
53
54
55
56
# File 'app/helpers/tramway/records_helper.rb', line 50

def index_path_of_model(model_class, tab, filter)
  if tab
    records_path model: model_class, filter: filter, scope: tab
  else
    records_path model: model_class, filter: filter
  end
end

#model_classObject



25
26
27
# File 'app/helpers/tramway/records_helper.rb', line 25

def model_class
  params[:model].constantize
end

#new_associated_record_path(object:, association:, as:) ⇒ Object



77
78
79
80
81
82
83
# File 'app/helpers/tramway/records_helper.rb', line 77

def new_associated_record_path(object:, association:, as:)
  unless association.options[:class_name].present?
    raise "You should set `class_name` for #{association.name} association"
  end

  new_record_path(**new_record_path_options(object, association, as))
end

#new_current_model_record_path(*args, **options) ⇒ Object



13
14
15
# File 'app/helpers/tramway/records_helper.rb', line 13

def new_current_model_record_path(*args, **options)
  new_record_path args, options.merge(model: params[:model])
end

#new_record_path_options(object, association, as) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/helpers/tramway/records_helper.rb', line 85

def new_record_path_options(object, association, as)
  hash = {
    model: association.class_name,
    redirect: current_model_record_path(object.model)
  }

  if association.options[:as].present? # polymorphic? conditiion
    hash.merge! options_for_polymorphic_link object, association
  else
    hash.merge! options_for_isomorphic_link object, association, as
  end

  hash
end


109
110
111
112
113
114
115
# File 'app/helpers/tramway/records_helper.rb', line 109

def options_for_isomorphic_link(object, association, as)
  {
    association.options[:class_name].underscore => {
      as || object.model.class.name.underscore.gsub('/', '_') => object.id
    }
  }
end


100
101
102
103
104
105
106
107
# File 'app/helpers/tramway/records_helper.rb', line 100

def options_for_polymorphic_link(object, association)
  {
    association.options[:class_name].underscore => {
      association.options[:as] => object.id,
      association.type => object.class.model_name
    }
  }
end

#public_path(record) ⇒ Object



21
22
23
# File 'app/helpers/tramway/records_helper.rb', line 21

def public_path(record)
  record.public_path || try("#{record.class.name.underscore.gsub('/', '_')}_path", record)
end

#search_tab_title(count) ⇒ Object



37
38
39
# File 'app/helpers/tramway/records_helper.rb', line 37

def search_tab_title(count)
  "#{t('helpers.scope.found')} / #{count}"
end

#searchable_model?(model_class) ⇒ Boolean



41
42
43
# File 'app/helpers/tramway/records_helper.rb', line 41

def searchable_model?(model_class)
  model_class.methods.include? :full_text_search
end

#tab_title(model_class, tab, count, _state_method = :state) ⇒ Object



66
67
68
69
70
# File 'app/helpers/tramway/records_helper.rb', line 66

def tab_title(model_class, tab, count, _state_method = :state)
  model = model_class.name.underscore
  name = collection_human_name model_name: model, collection_name: tab
  "#{name} / #{count}"
end

#there_any_filters?(model_class) ⇒ Boolean



117
118
119
# File 'app/helpers/tramway/records_helper.rb', line 117

def there_any_filters?(model_class)
  decorator_class(model_class).list_filters&.any?
end

#to_path(constant) ⇒ Object



33
34
35
# File 'app/helpers/tramway/records_helper.rb', line 33

def to_path(constant)
  constant.name.underscore.gsub '/', '_'
end