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.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/railspress.rb', line 31

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
  @admin_auth_concern = 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

#admin_auth_concernObject

Returns the value of attribute admin_auth_concern.



14
15
16
# File 'lib/railspress.rb', line 14

def admin_auth_concern
  @admin_auth_concern
end

#api_enabledObject (readonly)

Returns the value of attribute api_enabled.



29
30
31
# File 'lib/railspress.rb', line 29

def api_enabled
  @api_enabled
end

#author_class_nameObject

Returns the value of attribute author_class_name.



14
15
16
# File 'lib/railspress.rb', line 14

def author_class_name
  @author_class_name
end

#author_display_methodObject

Returns the value of attribute author_display_method.



14
15
16
# File 'lib/railspress.rb', line 14

def author_display_method
  @author_display_method
end

#author_scopeObject

Returns the value of attribute author_scope.



14
15
16
# File 'lib/railspress.rb', line 14

def author_scope
  @author_scope
end

#authors_enabledObject (readonly)

Returns the value of attribute authors_enabled.



29
30
31
# File 'lib/railspress.rb', line 29

def authors_enabled
  @authors_enabled
end

#blog_pathObject

Returns the value of attribute blog_path.



14
15
16
# File 'lib/railspress.rb', line 14

def blog_path
  @blog_path
end

#cms_enabledObject (readonly)

Returns the value of attribute cms_enabled.



29
30
31
# File 'lib/railspress.rb', line 29

def cms_enabled
  @cms_enabled
end

#current_api_actor_methodObject

Returns the value of attribute current_api_actor_method.



14
15
16
# File 'lib/railspress.rb', line 14

def current_api_actor_method
  @current_api_actor_method
end

#current_api_actor_procObject

Returns the value of attribute current_api_actor_proc.



14
15
16
# File 'lib/railspress.rb', line 14

def current_api_actor_proc
  @current_api_actor_proc
end

#current_author_methodObject

Returns the value of attribute current_author_method.



14
15
16
# File 'lib/railspress.rb', line 14

def current_author_method
  @current_author_method
end

#current_author_procObject

Returns the value of attribute current_author_proc.



14
15
16
# File 'lib/railspress.rb', line 14

def current_author_proc
  @current_author_proc
end

#default_index_columnsObject

Returns the value of attribute default_index_columns.



14
15
16
# File 'lib/railspress.rb', line 14

def default_index_columns
  @default_index_columns
end

#focal_points_enabledObject (readonly)

Returns the value of attribute focal_points_enabled.



29
30
31
# File 'lib/railspress.rb', line 29

def focal_points_enabled
  @focal_points_enabled
end

#image_contextsObject

Returns the value of attribute image_contexts.



29
30
31
# File 'lib/railspress.rb', line 29

def image_contexts
  @image_contexts
end

#inline_editing_checkObject

Returns the value of attribute inline_editing_check.



14
15
16
# File 'lib/railspress.rb', line 14

def inline_editing_check
  @inline_editing_check
end

#post_image_variantsObject

Returns the value of attribute post_image_variants.



14
15
16
# File 'lib/railspress.rb', line 14

def post_image_variants
  @post_image_variants
end

#post_images_enabledObject (readonly)

Returns the value of attribute post_images_enabled.



29
30
31
# File 'lib/railspress.rb', line 29

def post_images_enabled
  @post_images_enabled
end

#public_base_urlObject

Returns the value of attribute public_base_url.



14
15
16
# File 'lib/railspress.rb', line 14

def public_base_url
  @public_base_url
end

#words_per_minuteObject

Returns the value of attribute words_per_minute.



14
15
16
# File 'lib/railspress.rb', line 14

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



94
95
96
97
98
99
100
# File 'lib/railspress.rb', line 94

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



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

def enable_api
  @api_enabled = true
end

#enable_authorsObject

Declarative setter: config.enable_authors



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

def enable_authors
  @authors_enabled = true
end

#enable_cmsObject

Declarative setter: config.enable_cms



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

def enable_cms
  @cms_enabled = true
end

#enable_focal_pointsObject

Declarative setter: config.enable_focal_points



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

def enable_focal_points
  @focal_points_enabled = true
end

#enable_post_imagesObject

Declarative setter: config.enable_post_images



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

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



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

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)


172
173
174
# File 'lib/railspress.rb', line 172

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



149
150
151
# File 'lib/railspress.rb', line 149

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/railspress.rb', line 130

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



154
155
156
157
158
159
160
161
# File 'lib/railspress.rb', line 154

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



103
104
105
# File 'lib/railspress.rb', line 103

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.



81
82
83
84
85
86
# File 'lib/railspress.rb', line 81

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