Class: Railspress::FocalPoint
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Railspress::FocalPoint
- Defined in:
- app/models/railspress/focal_point.rb
Constant Summary collapse
- VALID_OVERRIDE_TYPES =
Valid override types
%w[focal crop upload].freeze
Instance Method Summary collapse
-
#clear_override(context) ⇒ Object
Clear override for context (revert to focal point).
-
#has_override?(context) ⇒ Boolean
Check if context has custom override (not using focal point).
-
#offset_from_center? ⇒ Boolean
Check if focal point differs from center.
-
#override_for(context) ⇒ Object
Get override for specific context.
-
#reset! ⇒ Object
Reset to center.
-
#set_override(context, data) ⇒ Object
Set override for context.
-
#to_css ⇒ Object
Get CSS object-position value.
-
#to_point ⇒ Object
Get focal point as hash.
Instance Method Details
#clear_override(context) ⇒ Object
Clear override for context (revert to focal point)
48 49 50 |
# File 'app/models/railspress/focal_point.rb', line 48 def clear_override(context) set_override(context, { "type" => "focal" }) end |
#has_override?(context) ⇒ Boolean
Check if context has custom override (not using focal point)
37 38 39 40 |
# File 'app/models/railspress/focal_point.rb', line 37 def has_override?(context) override = override_for(context) override.present? && override[:type] != "focal" end |
#offset_from_center? ⇒ Boolean
Check if focal point differs from center
27 28 29 |
# File 'app/models/railspress/focal_point.rb', line 27 def offset_from_center? (focal_x - 0.5).abs > 0.001 || (focal_y - 0.5).abs > 0.001 end |
#override_for(context) ⇒ Object
Get override for specific context
32 33 34 |
# File 'app/models/railspress/focal_point.rb', line 32 def override_for(context) overrides&.dig(context.to_s)&.with_indifferent_access end |
#reset! ⇒ Object
Reset to center
53 54 55 |
# File 'app/models/railspress/focal_point.rb', line 53 def reset! update!(focal_x: 0.5, focal_y: 0.5) end |
#set_override(context, data) ⇒ Object
Set override for context
43 44 45 |
# File 'app/models/railspress/focal_point.rb', line 43 def set_override(context, data) self.overrides = (overrides || {}).merge(context.to_s => data) end |
#to_css ⇒ Object
Get CSS object-position value
22 23 24 |
# File 'app/models/railspress/focal_point.rb', line 22 def to_css "object-position: #{(focal_x * 100).round(1)}% #{(focal_y * 100).round(1)}%" end |
#to_point ⇒ Object
Get focal point as hash
17 18 19 |
# File 'app/models/railspress/focal_point.rb', line 17 def to_point { x: focal_x, y: focal_y } end |