Module: NitroRails::NitroTagHelper

Defined in:
app/helpers/nitro_rails/nitro_tag_helper.rb

Instance Method Summary collapse

Instance Method Details

#body_options(**options) ⇒ Object



43
44
45
# File 'app/helpers/nitro_rails/nitro_tag_helper.rb', line 43

def body_options(**options)
  @custom_body_options = options
end

#current_nitro_resourceObject



96
97
98
# File 'app/helpers/nitro_rails/nitro_tag_helper.rb', line 96

def current_nitro_resource
  @_nitro_resource_stack&.last
end

#nitro_attribute(resource = current_nitro_resource, attribute, **options) ⇒ Object



144
145
146
# File 'app/helpers/nitro_rails/nitro_tag_helper.rb', line 144

def nitro_attribute(resource = current_nitro_resource, attribute, **options)
  nitro_attribute_tag(options.delete(:tag) || :p, resource, attribute, **options)
end

#nitro_attribute_tag(tag_or_resource = :p, maybe_resource = current_nitro_resource, attribute, **options, &block) ⇒ Object

ATTRIBUTE ##



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
# File 'app/helpers/nitro_rails/nitro_tag_helper.rb', line 104

def nitro_attribute_tag(tag_or_resource = :p, maybe_resource = current_nitro_resource, attribute, **options, &block)
  # Not sure this works for all cases
  # nitro_attribute_tag(:p, @task, :name, **options) 
  # nitro_attribute_tag(@task, :name, **options)
  # nitro_attribute_tag(:name, **options)
  # nitro_attribute_tag(:p, :name, **options)
  # 
  ## First argument can be tag, resource, or attribute
  ## Second argument can be resource or attribute
  ## Third argument is always attribute
  if tag_or_resource.is_a?(Symbol) || tag_or_resource.is_a?(String)
    tag = tag_or_resource
    resource = maybe_resource
  else
    tag = :p
    resource = tag_or_resource
  end

  nitro_options(options) do |options|
    options.default(:nitro_id, resource.nitro_id(attribute))
    options.concat(:class, classify(attribute))
    options.concat(:class, classify(resource.type, attribute))
    options.concat(:class, attribute_status_class(resource, attribute))
  end

  # NEW CODE 
  value = resource.send(attribute)

  if options[:format] && value.present?
    options.delete(:format).each do |method, params|
      value = value.send(method, *params)
    end
  end

  value = value.presence || options.delete(:default) || ""
  # END NEW CODE

  nitro_tag(tag, value, **options, &block)
end

#nitro_body_tag(**options, &block) ⇒ Object

BODY ##



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/nitro_rails/nitro_tag_helper.rb', line 15

def nitro_body_tag(**options, &block) 
  options.merge!(@custom_body_options) if @custom_body_options

  nitro_options(options, @custom_body_options) do |options|
    options.concat(:class, options.delete(:color_scheme) || current_color_scheme)
    options.concat(:class, "#{controller_name}-#{action_name}")

    if turbo_native_app?
      options.concat(:class, "turbo-native")

      if ios_app?
        options.concat(:class, "ios")
        options.override(:data, :turbo_native, "iOS")
      elsif android_app?
        options.concat(:class, "android")
        options.override(:data, :turbo_native, "Android")
      end
    end

    options.concat(:data, :controller, "panel-manager time-zone nitro")
    options.concat(:data, :action, "turbo@morph->panel-manager#morph click->panel-manager#closeOtherPanels touchend->panel-manager#closeOtherPanels")
    options.default(:data, :panel_manager_target, "container")
    options.default(:data, :time_zone_server_time_value, "#{Time.now.utc.iso8601}")
  end

  (:body, **options, &block) # Should use nitro_tag
end

LINK ##



51
52
53
54
55
56
57
58
# File 'app/helpers/nitro_rails/nitro_tag_helper.rb', line 51

def nitro_link_tag(*args, **options, &block)
  nitro_options(options) do |options|
    options.concat(:class, "nitro-link")
    options.concat(:class, "active") if current_page?(args.last)
  end

  link_to(*args, **options, &block)
end

#nitro_resource(resource, **options, &block) ⇒ Object



85
86
87
# File 'app/helpers/nitro_rails/nitro_tag_helper.rb', line 85

def nitro_resource(resource, **options, &block) 
  nitro_resource_tag(options.delete(:tag) || :div, resource, **options, &block)
end

#nitro_resource_tag(tag = :div, resource, **options, &block) ⇒ Object

RESOURCE ##



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

def nitro_resource_tag(tag = :div, resource, **options, &block)
  nitro_options(options) do |options|
    options.default(:nitro_id, resource.nitro_id) # Maybe add default?
    options.default(:data, :id, resource.id)
    options.concat(:class, classify(resource.type))
    options.concat(:class, resource_status_classes(resource))
    options.concat(:class, "hover-context") unless options[:hover_context] == false

    if link = options.delete(:link) 
      options.default(:data, :action, "click->nitro#link")
      options.default(:data, :nitro_link, link == true ? url_for(resource) : link)
    end
  end

  with_nitro_resource(resource) do
    nitro_tag(tag, **options, &block)
  end
end

#nitro_tag(tag = :div, text = nil, nitro_id: nil, link: nil, **options, &block) ⇒ Object



3
4
5
6
7
8
9
# File 'app/helpers/nitro_rails/nitro_tag_helper.rb', line 3

def nitro_tag(tag = :div, text = nil, nitro_id: nil, link: nil, **options, &block)
  nitro_options(options) do |options| 
    options.default(:data, :nitro_id, nitro_id) if nitro_id
  end

  (tag, text, **options, &block)
end

#with_nitro_resource(resource) ⇒ Object



89
90
91
92
93
94
# File 'app/helpers/nitro_rails/nitro_tag_helper.rb', line 89

def with_nitro_resource(resource)
  (@_nitro_resource_stack ||= []).push(resource)
  yield
ensure
  @_nitro_resource_stack.pop
end