Class: Frontyard::ApplicationComponent

Inherits:
Phlex::HTML
  • Object
show all
Includes:
Phlex::Rails::Helpers::ButtonTo, Phlex::Rails::Helpers::DOMID, Phlex::Rails::Helpers::LinkTo, Phlex::Rails::Helpers::Routes
Defined in:
app/components/frontyard/application_component.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configObject



33
34
35
# File 'app/components/frontyard/application_component.rb', line 33

def config
  @config ||= default_config
end

.default_configObject



11
12
13
14
15
# File 'app/components/frontyard/application_component.rb', line 11

def default_config
  @default_config ||= Config.init(
    class: generate_css_class
  )
end

.generate_css_classObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/components/frontyard/application_component.rb', line 17

def generate_css_class
  return "" if name == "Frontyard::ApplicationComponent"
  class_name = name
  if class_name.to_s.start_with?("Frontyard::") && class_name.to_s != "Frontyard::ApplicationComponent"
    return class_name.split("::").map { |part| part.gsub(/([A-Z])/, '-\1').downcase.sub(/^-/, "") }.join("-")
  end
  return "frontyard-component" if class_name.nil? || class_name.start_with?("#<")
  this_css_class = class_name.split("::").last.gsub(/([A-Z])/, '-\1').downcase.sub(/^-/, "")
  if superclass.respond_to?(:generate_css_class)
    css_class = Contours::StructuredString.new(superclass.generate_css_class)
    css_class << this_css_class
  else
    this_css_class
  end.to_s
end

.initialize_with(*keys, **kwargs, &block) ⇒ Object

Define an initialize method along with attr_accessors

Example:

initialize_with :some, :keyword, optional: nil
initialize_with :some, :keyword, optional: nil do
  @processed_data = some.upcase
end


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/components/frontyard/application_component.rb', line 67

def self.initialize_with(*keys, **kwargs, &block)
  mod = Module.new
  tokens = keys.map do |key|
    [
      "#{key}:",
      "@#{key} = #{key}"
    ]
  end.to_h
  kwargs.each do |key, value|
    value ||= "nil"
    tokens["#{key}: #{value}"] = "@#{key} = #{key}"
  end
  accessors = keys + kwargs.keys
  keyword_args = tokens.keys
  ivars = tokens.values

  # Include the block code in the initialize method if provided
  block_code = if block_given?
    # Convert the block to a string representation that can be executed
    # This is a simplified approach - in practice you might want to use a more robust method
    "instance_eval(&self.class.instance_variable_get(:@initialize_block))"
  else
    ""
  end

  mod.class_eval <<~RUBY, __FILE__, __LINE__ + 1
    def initialize(#{keyword_args.join(", ")}, **options, &block)
      #{ivars.join("\n")}
      @flash = options.delete(:flash) || {}
      if options.key?(:html_options)
        @html_options = options.delete(:html_options)
      end
      @options = options
      yield self if block_given?
      #{block_code}
    end
  RUBY
  accessors += [:flash, :options, :html_options]
  attr_accessor(*accessors)
  const_set(:Initializer, mod)
  include mod

  # Store the block for execution on instances
  @initialize_block = block if block_given?
end

Instance Method Details

#before_templateObject



44
45
46
47
48
49
# File 'app/components/frontyard/application_component.rb', line 44

def before_template
  if Rails.env.development?
    comment { "Before #{self.class.name}" }
  end
  super
end

#html_optionsObject



51
52
53
# File 'app/components/frontyard/application_component.rb', line 51

def html_options
  @html_options || self.class.config
end

#namespaceObject



55
56
57
58
# File 'app/components/frontyard/application_component.rb', line 55

def namespace = @namespace ||= begin
  ns = self.class.name.deconstantize
  ns.empty? ? Object : Object.const_get(ns)
end

#paramsObject



184
# File 'app/components/frontyard/application_component.rb', line 184

def params = view_context.params

#render_model(from: nil, **kwargs) ⇒ Object

Rendere the partials that represent a model

Example:

render_model something: value
render_model something: value, from: "widgets/widget"


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/components/frontyard/application_component.rb', line 136

def render_model(from: nil, **kwargs)
  const_names = if from
    base = from.classify
    [base, "#{base}Component"]
  else
    namespace = self.class.name.deconstantize
    model_name = namespace.demodulize.singularize
    [
      "#{namespace}::#{model_name}",
      "#{namespace}::#{model_name}Component"
    ]
  end
  klass = safe_const_get(*const_names)
  filtered_kwargs = filter_kwargs_for(klass, kwargs)
  render klass.new(**filtered_kwargs)
end

#render_table(from: nil, **kwargs, &block) ⇒ Object

Render a table component

Example:

render_table from: "widgets/widget"
render_table from: "widgets/widget", columns: [:name, :description]


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/components/frontyard/application_component.rb', line 158

def render_table(from: nil, **kwargs, &block)
  const_names = if from
    base = from.classify
    [base, "#{base}Table"]
  elsif self.class.name.include?("::")
    # For top-level classes (no namespace), try Frontyard namespace first
    namespace = self.class.name.deconstantize
    [
      "#{namespace}::Table",
      "#{namespace}::#{self.class.name.demodulize}Table",
      "#{self.class.name}Table"
    ]
  else
    # Top-level class, try Frontyard namespace with common table names
    [
      "Frontyard::TestTable",
      "Frontyard::Table",
      "Frontyard::#{self.class.name}Table",
      "#{self.class.name}Table"
    ]
  end
  klass = safe_const_get(*const_names)
  filtered_kwargs = filter_kwargs_for(klass, kwargs)
  render klass.new(**filtered_kwargs, &block)
end

#view_templateObject



186
# File 'app/components/frontyard/application_component.rb', line 186

def view_template(&) = div(**html_options, &)