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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/railspress.rb', line 23

def initialize
  @authors_enabled = false
  @post_images_enabled = false
  @focal_points_enabled = false
  @cms_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
  @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

#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.



21
22
23
# File 'lib/railspress.rb', line 21

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.



21
22
23
# File 'lib/railspress.rb', line 21

def cms_enabled
  @cms_enabled
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.



21
22
23
# File 'lib/railspress.rb', line 21

def focal_points_enabled
  @focal_points_enabled
end

#image_contextsObject

Returns the value of attribute image_contexts.



21
22
23
# File 'lib/railspress.rb', line 21

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.



21
22
23
# File 'lib/railspress.rb', line 21

def post_images_enabled
  @post_images_enabled
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



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

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_authorsObject

Declarative setter: config.enable_authors



42
43
44
# File 'lib/railspress.rb', line 42

def enable_authors
  @authors_enabled = true
end

#enable_cmsObject

Declarative setter: config.enable_cms



57
58
59
# File 'lib/railspress.rb', line 57

def enable_cms
  @cms_enabled = true
end

#enable_focal_pointsObject

Declarative setter: config.enable_focal_points



52
53
54
# File 'lib/railspress.rb', line 52

def enable_focal_points
  @focal_points_enabled = true
end

#enable_post_imagesObject

Declarative setter: config.enable_post_images



47
48
49
# File 'lib/railspress.rb', line 47

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



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

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)


154
155
156
# File 'lib/railspress.rb', line 154

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



131
132
133
# File 'lib/railspress.rb', line 131

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/railspress.rb', line 112

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



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

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



85
86
87
# File 'lib/railspress.rb', line 85

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.



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

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