Class: Satis::Helpers::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/satis/helpers/container.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action_view) ⇒ Container

Returns a new instance of Container.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/satis/helpers/container.rb', line 8

def initialize(action_view)
  @action_view = action_view

  add_helper :appearance_switcher, Satis::AppearanceSwitcher::Component
  add_helper :avatar, Satis::Avatar::Component
  add_helper :breadcrumbs, Satis::Breadcrumbs::Component
  add_helper :card, Satis::Card::Component
  add_helper :call_to_action, Satis::CallToAction::Component
  add_helper :editor, Satis::Editor::Component
  add_helper :flash_messages, Satis::FlashMessages::Component
  add_helper :info, Satis::Info::Component
  add_helper :map, Satis::Map::Component
  add_helper :menu, Satis::Menu::Component
  add_helper :page, Satis::Page::Component
  add_helper :sidebar_menu, Satis::SidebarMenu::Component
  add_helper :tabs, Satis::Tabs::Component
  add_helper :input, Satis::Input::Component
  add_helper :progress_bar, Satis::ProgressBar::Component
  add_helper :dialog, Satis::Dialog::Component
end

Instance Attribute Details

#action_viewObject (readonly)

Returns the value of attribute action_view.



4
5
6
# File 'lib/satis/helpers/container.rb', line 4

def action_view
  @action_view
end

Class Method Details

.add_helper(name, component) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/satis/helpers/container.rb', line 66

def self.add_helper(name, component)
  if respond_to?(name)
    Satis.config.logger.warn("Helper #{name} already defined, skipping.")
    return
  end
  define_method(name) do |*args, **kwargs, &block|
    original_args = args.dup
    options = args.extract_options!
    instance = if options.key? :variant
      variant_component = component.to_s.sub(/::Component$/, "::#{options[:variant].to_s.camelize}::Component").safe_constantize
      (variant_component || component).new(*original_args, **kwargs)
    else
      component.new(*original_args, **kwargs)
    end
    instance.original_view_context = action_view
    action_view.render(instance, &block)
  end
end

Instance Method Details

#browserObject



35
36
37
# File 'lib/satis/helpers/container.rb', line 35

def browser
  @browser ||= Browser.new(action_view.request.user_agent)
end

#copyable(name, scrub: "#") ⇒ Object



29
30
31
32
33
# File 'lib/satis/helpers/container.rb', line 29

def copyable(name, scrub: "#")
  return if name.blank?

  action_view.("satis-copyable", name, scrub: scrub)
end

#form_for(name, *args) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/satis/helpers/container.rb', line 39

def form_for(name, *args, &)
  options = args.extract_options!.deep_merge!(html: {data: {}})
  form_options_defaults!(options)
  update_form_data_options!(options[:html][:data], options)
  args << options.merge(builder: Satis::Forms::Builder)
  action_view.form_for(name, *args, &)
end

#form_options_defaults!(options) ⇒ Object



54
55
56
57
# File 'lib/satis/helpers/container.rb', line 54

def form_options_defaults!(options)
  options[:submit_on_enter] = Satis.submit_on_enter? if options[:submit_on_enter].nil?
  options[:confirm_before_leave] = Satis.confirm_before_leave? if options[:confirm_before_leave].nil?
end

#form_with(model: nil, scope: nil, url: nil, format: nil, **options) ⇒ Object



47
48
49
50
51
52
# File 'lib/satis/helpers/container.rb', line 47

def form_with(model: nil, scope: nil, url: nil, format: nil, **options, &)
  options = options.reverse_merge(builder: Satis::Forms::Builder, class: "").deep_merge!(data: {})
  form_options_defaults!(options)
  update_form_data_options!(options[:data], options)
  action_view.form_with(model: model, scope: scope, url: url, format: format, **options, &)
end

#update_form_data_options!(data, options) ⇒ Object



59
60
61
62
63
64
# File 'lib/satis/helpers/container.rb', line 59

def update_form_data_options!(data, options)
  data[:controller] ||= ""
  data[:controller] += " form"
  data[:"form-no-submit-on-enter-value"] = !options[:submit_on_enter]
  data[:"form-confirm-before-leave-value"] = options[:confirm_before_leave]
end