Class: Railspress::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/railspress.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/railspress.rb', line 26

def initialize
  @authors_enabled = false
  @post_images_enabled = false
  @focal_points_enabled = false
  @cms_enabled = false
  @api_enabled = false
  @image_contexts = default_image_contexts
  @post_image_variants = {}
  @inline_editing_check = nil
  @author_class_name = "User"
  @current_author_method = :current_user
  @current_author_proc = nil
  @current_api_actor_method = :current_user
  @current_api_actor_proc = nil
  @public_base_url = nil
  @author_scope = nil
  @author_display_method = :name
  @words_per_minute = 200
  @blog_path = "/blog"
  @default_index_columns = [ :id, :title, :name, :created_at ]
end

Instance Attribute Details

#api_enabledObject (readonly)

Returns the value of attribute api_enabled.



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

def api_enabled
  @api_enabled
end

#author_class_nameObject

Returns the value of attribute author_class_name.



10
11
12
# File 'lib/railspress.rb', line 10

def author_class_name
  @author_class_name
end

#author_display_methodObject

Returns the value of attribute author_display_method.



10
11
12
# File 'lib/railspress.rb', line 10

def author_display_method
  @author_display_method
end

#author_scopeObject

Returns the value of attribute author_scope.



10
11
12
# File 'lib/railspress.rb', line 10

def author_scope
  @author_scope
end

#authors_enabledObject (readonly)

Returns the value of attribute authors_enabled.



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

def authors_enabled
  @authors_enabled
end

#blog_pathObject

Returns the value of attribute blog_path.



10
11
12
# File 'lib/railspress.rb', line 10

def blog_path
  @blog_path
end

#cms_enabledObject (readonly)

Returns the value of attribute cms_enabled.



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

def cms_enabled
  @cms_enabled
end

#current_api_actor_methodObject

Returns the value of attribute current_api_actor_method.



10
11
12
# File 'lib/railspress.rb', line 10

def current_api_actor_method
  @current_api_actor_method
end

#current_api_actor_procObject

Returns the value of attribute current_api_actor_proc.



10
11
12
# File 'lib/railspress.rb', line 10

def current_api_actor_proc
  @current_api_actor_proc
end

#current_author_methodObject

Returns the value of attribute current_author_method.



10
11
12
# File 'lib/railspress.rb', line 10

def current_author_method
  @current_author_method
end

#current_author_procObject

Returns the value of attribute current_author_proc.



10
11
12
# File 'lib/railspress.rb', line 10

def current_author_proc
  @current_author_proc
end

#default_index_columnsObject

Returns the value of attribute default_index_columns.



10
11
12
# File 'lib/railspress.rb', line 10

def default_index_columns
  @default_index_columns
end

#focal_points_enabledObject (readonly)

Returns the value of attribute focal_points_enabled.



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

def focal_points_enabled
  @focal_points_enabled
end

#image_contextsObject

Returns the value of attribute image_contexts.



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

def image_contexts
  @image_contexts
end

#inline_editing_checkObject

Returns the value of attribute inline_editing_check.



10
11
12
# File 'lib/railspress.rb', line 10

def inline_editing_check
  @inline_editing_check
end

#post_image_variantsObject

Returns the value of attribute post_image_variants.



10
11
12
# File 'lib/railspress.rb', line 10

def post_image_variants
  @post_image_variants
end

#post_images_enabledObject (readonly)

Returns the value of attribute post_images_enabled.



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

def post_images_enabled
  @post_images_enabled
end

#public_base_urlObject

Returns the value of attribute public_base_url.



10
11
12
# File 'lib/railspress.rb', line 10

def public_base_url
  @public_base_url
end

#words_per_minuteObject

Returns the value of attribute words_per_minute.



10
11
12
# File 'lib/railspress.rb', line 10

def words_per_minute
  @words_per_minute
end

Instance Method Details

#add_image_context(name, aspect:, label: nil, sizes: []) ⇒ Object

Add a single image context



88
89
90
91
92
93
94
# File 'lib/railspress.rb', line 88

def add_image_context(name, aspect:, label: nil, sizes: [])
  @image_contexts[name.to_sym] = {
    aspect: aspect,
    label: label || name.to_s.humanize,
    sizes: sizes
  }
end

#enable_apiObject

Declarative setter: config.enable_api



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

def enable_api
  @api_enabled = true
end

#enable_authorsObject

Declarative setter: config.enable_authors



49
50
51
# File 'lib/railspress.rb', line 49

def enable_authors
  @authors_enabled = true
end

#enable_cmsObject

Declarative setter: config.enable_cms



64
65
66
# File 'lib/railspress.rb', line 64

def enable_cms
  @cms_enabled = true
end

#enable_focal_pointsObject

Declarative setter: config.enable_focal_points



59
60
61
# File 'lib/railspress.rb', line 59

def enable_focal_points
  @focal_points_enabled = true
end

#enable_post_imagesObject

Declarative setter: config.enable_post_images



54
55
56
# File 'lib/railspress.rb', line 54

def enable_post_images
  @post_images_enabled = true
end

#entity_for(route_key) ⇒ Object

Find entity config by route key - resolves fresh on each call



158
159
160
161
162
163
# File 'lib/railspress.rb', line 158

def entity_for(route_key)
  registration = entity_registrations[route_key.to_s]
  return nil unless registration

  resolve_entity(registration[:class_name], registration[:options])
end

#entity_registered?(route_key) ⇒ Boolean

Check if an entity is registered

Returns:

  • (Boolean)


166
167
168
# File 'lib/railspress.rb', line 166

def entity_registered?(route_key)
  entity_registrations.key?(route_key.to_s)
end

#entity_registrationsObject

Entity registrations: route_key => { class_name:, options: } We store class names (strings), not config objects, so Rails reloading works



143
144
145
# File 'lib/railspress.rb', line 143

def entity_registrations
  @entity_registrations ||= {}
end

#register_entity(identifier, options = {}) ⇒ Object

Register a host model as a CMS-managed entity

Accepts class, string, or symbol. String/symbol registration is preferred for Rails reloader compatibility in development.

Registration is deferred until first access - this allows registration in initializers before models are loaded by Zeitwerk.

Examples:

String registration (preferred)

config.register_entity "Project"
config.register_entity "Portfolio", label: "Work Samples"

Symbol registration

config.register_entity :project
config.register_entity :admin_portfolio, label: "Work Samples"

Class registration (works but may have stale refs after reload)

config.register_entity Project

Parameters:

  • identifier (Class, String, Symbol)

    The model to register

  • options (Hash) (defaults to: {})

    Optional configuration

Options Hash (options):

  • :label (String)

    Custom sidebar/header label



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/railspress.rb', line 124

def register_entity(identifier, options = {})
  # Normalize to class name string
  class_name = case identifier
  when String then identifier
  when Symbol then identifier.to_s.camelize
  when Class  then identifier.name
  else
                 raise ArgumentError, "Expected String, Symbol, or Class, got #{identifier.class}"
  end

  # Compute route_key from class name (e.g., "Project" -> "projects")
  route_key = class_name.underscore.pluralize

  # Store just the class name and options - resolved fresh on each access
  entity_registrations[route_key] = { class_name: class_name, options: options }
end

#registered_entitiesObject

Get all registered entities - resolves fresh on each call



148
149
150
151
152
153
154
155
# File 'lib/railspress.rb', line 148

def registered_entities
  result = {}
  entity_registrations.each do |route_key, registration|
    config = resolve_entity(registration[:class_name], registration[:options])
    result[route_key] = config if config
  end
  result
end

#remove_image_context(name) ⇒ Object

Remove an image context



97
98
99
# File 'lib/railspress.rb', line 97

def remove_image_context(name)
  @image_contexts.delete(name.to_sym)
end

#validate!Object

Validate configuration after the configure block completes. This keeps the configure block order-independent.



75
76
77
78
79
80
# File 'lib/railspress.rb', line 75

def validate!
  if @inline_editing_check && !@cms_enabled
    raise Railspress::ConfigurationError,
      "Inline editing requires CMS. Add `config.enable_cms` to your initializer."
  end
end