Class: Railsui::Configuration

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, Thor::Actions
Defined in:
lib/railsui/configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



14
15
16
17
18
19
20
21
22
# File 'lib/railsui/configuration.rb', line 14

def initialize(options = {})
  options ||= {}
  assign_attributes(options)
  self.application_name ||= "Rails UI"
  self.support_email ||= "support@example.com"
  self.build_mode ||= detect_build_mode
  theme
  initialize_theme_classes if theme
end

Instance Attribute Details

#application_nameObject

Returns the value of attribute application_name.



11
12
13
# File 'lib/railsui/configuration.rb', line 11

def application_name
  @application_name
end

#body_classesObject

Returns the value of attribute body_classes.



11
12
13
# File 'lib/railsui/configuration.rb', line 11

def body_classes
  @body_classes
end

#build_modeObject

Returns the value of attribute build_mode.



11
12
13
# File 'lib/railsui/configuration.rb', line 11

def build_mode
  @build_mode
end

#pagesObject



65
66
67
# File 'lib/railsui/configuration.rb', line 65

def pages
  Array.wrap(@pages)
end

#support_emailObject

Returns the value of attribute support_email.



11
12
13
# File 'lib/railsui/configuration.rb', line 11

def support_email
  @support_email
end

#themeObject

Returns the value of attribute theme.



11
12
13
# File 'lib/railsui/configuration.rb', line 11

def theme
  @theme
end

Class Method Details

.config_pathObject



69
70
71
# File 'lib/railsui/configuration.rb', line 69

def self.config_path
  Rails.root.join("config", "railsui.yml")
end

.convert_keys_to_strings(hash) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/railsui/configuration.rb', line 137

def self.convert_keys_to_strings(hash)
  hash.each_with_object({}) do |(key, value), result|
    new_key = key.to_s
    new_value = value.is_a?(Hash) ? convert_keys_to_strings(value) : value
    result[new_key] = new_value
  end
end

.delete_page(page) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/railsui/configuration.rb', line 45

def self.delete_page(page)
  # update config
  config_path = Rails.root.join("config", "railsui.yml")
  if File.exist?(config_path)
    config = Psych.safe_load_file(config_path, permitted_classes: [Hash, Railsui::Configuration])
    config.pages.delete(page)
    File.write(config_path, config.to_yaml)
    Railsui.restart
  end

  # remove view
  view_path = Rails.root.join("app", "views", "rui", "pages", "#{page}.html.erb")

  if File.exist?(view_path)
    File.delete(view_path)
  else
    puts "View #{view_path} does not exist"
  end
end

.load!Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/railsui/configuration.rb', line 28

def self.load!
  if File.exist?(config_path)
    begin
      config = Psych.load_file(config_path, permitted_classes: [Hash, Railsui::Configuration])
      return config if config.is_a?(Railsui::Configuration)

      new(config)
    rescue Psych::SyntaxError => e
      raise "❌ Failed to parse config/railsui.yml: #{e.message}. Please check the YAML syntax."
    rescue => e
      raise "❌ Failed to load Rails UI configuration: #{e.message}"
    end
  else
    new
  end
end

.synchronize_pagesObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/railsui/configuration.rb', line 114

def self.synchronize_pages
  config_path = Rails.root.join("config", "railsui.yml")
  return unless File.exist?(config_path)

  loaded_config = Psych.safe_load_file(config_path, permitted_classes: [Hash, Railsui::Configuration])

  # Ensure that the loaded configuration is an instance of Railsui::Configuration
  config = loaded_config.is_a?(Railsui::Configuration) ? loaded_config : new(loaded_config)

  existing_pages_in_dir = Dir[File.join(Railsui::Pages::VIEWS_FOLDER, "*.html.erb")].map do |filepath|
    File.basename(filepath, ".html.erb")
  end

  # Combine and sort the pages
  combined_pages = (config.pages + existing_pages_in_dir).uniq.sort

  # Update the configuration instance
  config.pages = combined_pages

  # Save the updated configuration back to the file
  File.write(config_path, config.to_yaml)
end

.update(params = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/railsui/configuration.rb', line 100

def self.update(params = {})
  begin
    config = load!
    config.assign_attributes(params)
    config.pages = Railsui::Pages.theme_pages.keys
    config.save

    Railsui.run_command "rails g railsui:update"
  rescue => e
    puts "❌ Failed to update configuration: #{e.message}"
    raise
  end
end

Instance Method Details

#build?Boolean

Check if build mode is build

Returns:

  • (Boolean)


189
190
191
# File 'lib/railsui/configuration.rb', line 189

def build?
  build_mode.to_s == "build"
end

#initialize_theme_classesObject



24
25
26
# File 'lib/railsui/configuration.rb', line 24

def initialize_theme_classes
  self.body_classes ||= Railsui::Themes.body_classes(theme)
end

#nobuild?Boolean

Check if build mode is nobuild

Returns:

  • (Boolean)


184
185
186
# File 'lib/railsui/configuration.rb', line 184

def nobuild?
  build_mode.to_s == "nobuild"
end

#save(skip_build: false) ⇒ Object



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
# File 'lib/railsui/configuration.rb', line 73

def save(skip_build: false)
  # Validate configuration before saving
  validate_configuration!

  begin
    # Create config/railsui.yml
    File.write(self.class.config_path, to_yaml)
  rescue => e
    raise "❌ Failed to save configuration to config/railsui.yml: #{e.message}"
  end

  # Change the Rails UI config to the latest version
  Railsui.config = self

  sleep 1

  # Only build CSS if not explicitly skipped and CSS file exists
  if !skip_build && should_build_css? && File.exist?(Rails.root.join("app/assets/tailwind/application.css"))
    begin
      Railsui.build_css
    rescue => e
      puts "⚠️  Warning: CSS build failed: #{e.message}"
      puts "You can rebuild manually with: rails tailwindcss:build"
    end
  end
end

#validate_configuration!Object

Validate entire configuration



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/railsui/configuration.rb', line 155

def validate_configuration!
  errors = []

  errors << "Application name cannot be blank" if application_name.blank?
  errors << "Support email cannot be blank" if support_email.blank?
  errors << "Theme cannot be blank" if theme.blank?

  # Validate theme exists (check against available themes)
  available_themes = ["hound", "shepherd", "corgie"]
  unless available_themes.include?(theme)
    errors << "Invalid theme: '#{theme}'. Available themes: #{available_themes.join(', ')}"
  end

  # Validate build mode
  unless ["build", "nobuild"].include?(build_mode.to_s)
    errors << "Invalid build_mode: '#{build_mode}'. Must be 'build' or 'nobuild'"
  end

  # Validate email format
  unless support_email =~ URI::MailTo::EMAIL_REGEXP
    errors << "Invalid email format for support_email: '#{support_email}'"
  end

  if errors.any?
    raise ArgumentError, "❌ Configuration validation failed:\n  - #{errors.join("\n  - ")}"
  end
end