Skip to content

Commit

Permalink
rename to format_using
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianthedev committed Jun 26, 2023
1 parent 575689d commit f6e2b3f
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 27 deletions.
12 changes: 5 additions & 7 deletions lib/avo/fields/base_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,9 @@ def value(property = nil)
final_value = instance_exec(@model, @resource, @view, self, &block)
end

if @format_as.present?
Avo::ExecutionContext.new(target: @format_as, model: model, key: property, value: final_value, resource: resource, view: view, field: self).handle
elsif @format_using.present?
# Run the value through resolver if present
instance_exec(final_value, &@format_using)
if @format_using.present?
# Apply the changes in the
Avo::ExecutionContext.new(target: @format_using, model: model, key: property, value: final_value, resource: resource, view: view, field: self).handle
else
final_value
end
Expand All @@ -199,8 +197,8 @@ def value(property = nil)
def fill_field(model, key, value, params)
return model unless model.methods.include? key.to_sym

if @update_as.present?
value = update_as(model, key, value, params)
if @update_using.present?
value = update_using(model, key, value, params)
end

model.public_send("#{key}=", value)
Expand Down
6 changes: 3 additions & 3 deletions spec/dummy/app/avo/resources/city_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CityResource < Avo::BaseResource
self.search_result_path = -> {
avo.resources_city_path record, custom: "yup"
}
self.extra_params = [city: [:name, :metadata, :coordinates, :city_center_area, :description, :population, :is_capital, :image_url, :tiny_description, :status, :features, features: {}, metadata: {}]]
self.extra_params = [city: [:name, :metadata, :coordinates, :city_center_area, :description, :population, :is_capital, :image_url, :tiny_description, :status, features: {}, metadata: {}]]
self.default_view_type = :map
self.map_view = {
mapkick_options: {
Expand Down Expand Up @@ -45,14 +45,14 @@ class CityResource < Avo::BaseResource
field :description, as: :trix, attachment_key: :description_file, visible: ->(resource:) { resource.params[:show_native_fields].blank? }
field :metadata,
as: :code,
format_as: -> {
format_using: -> {
if view == :edit
JSON.generate(value)
else
value
end
},
update_as: -> do
update_using: -> do
ActiveSupport::JSON.decode(value)
end
with_options hide_on: :forms do
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/avo/resources/comment_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CommentResource < Avo::BaseResource
self.after_update_path = :index

field :id, as: :id
field :body, as: :textarea, format_using: ->(value) do
field :body, as: :textarea, format_using: -> do
if view == :show
content_tag(:div, style: "white-space: pre-line") { value }
else
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/avo/resources/photo_comment_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PhotoCommentResource < Avo::BaseResource
end

field :id, as: :id
field :body, as: :textarea, format_using: ->(value) do
field :body, as: :textarea, format_using: -> do
if view == :show
content_tag(:div, style: "white-space: pre-line") { value }
else
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/avo/resources/post_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class PostResource < Avo::BaseResource
enforce_suggestions: true,
help: "The only allowed values here are `one`, `two`, and `three`"
field :cover_photo, as: :file, is_image: true, as_avatar: :rounded, full_width: true, hide_on: [], accept: "image/*", display_filename: false
field :cover_photo, as: :external_image, name: "Cover photo", required: true, hide_on: :all, link_to_resource: true, as_avatar: :rounded, format_using: ->(value) { value.present? ? value&.url : nil }
field :cover_photo, as: :external_image, name: "Cover photo", required: true, hide_on: :all, link_to_resource: true, as_avatar: :rounded, format_using: -> { value.present? ? value&.url : nil }
field :audio, as: :file, is_audio: true, accept: "audio/*"
field :excerpt, as: :text, hide_on: :all, as_description: true do |model|
extract_excerpt model.body
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/avo/resources/product_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ProductResource < Avo::BaseResource
}
}
title :title, as: :text
body :description, as: :textarea, format_using: ->(value) {
body :description, as: :textarea, format_using: -> {
simple_format value
}
end
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/avo/resources/team_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TeamResource < Avo::BaseResource
rows: 5,
readonly: false,
hide_on: :index,
format_using: ->(value) { value.to_s.truncate 30 },
format_using: -> { value.to_s.truncate 30 },
default: "This is a wonderful team!",
nullable: true,
null_values: ["0", "", "null", "nil"]
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/avo/resources/user_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class UserResource < Avo::BaseResource
hide_on: :edit do |model, resource, view, field|
model.posts.to_a.size > 0 ? "yes" : "no"
end
field :outside_link, as: :text, only_on: [:show], format_using: ->(url) { link_to("hey", url, target: "_blank") } do |model, *args|
field :outside_link, as: :text, only_on: [:show], format_using: -> { link_to("hey", value, target: "_blank") } do |model, *args|
main_app.hey_url
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/avo/resources/z_post_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ZPostResource < Avo::BaseResource
enforce_suggestions: true,
help: "The only allowed values here are `one`, `two`, and `three`"
field :cover_photo, as: :file, is_image: true, as_avatar: :rounded, full_width: true, hide_on: [], accept: "image/*"
field :cover_photo, as: :external_image, name: "Cover photo", required: true, hide_on: :all, link_to_resource: true, as_avatar: :rounded, format_using: ->(value) { value.present? ? value&.url : nil }
field :cover_photo, as: :external_image, name: "Cover photo", required: true, hide_on: :all, link_to_resource: true, as_avatar: :rounded, format_using: -> { value.present? ? value&.url : nil }
field :audio, as: :file, is_audio: true, accept: "audio/*"
field :excerpt, as: :text, hide_on: :all, as_description: true do |model|
ActionView::Base.full_sanitizer.sanitize(model.body).truncate 130
Expand Down
14 changes: 7 additions & 7 deletions spec/dummy/app/models/city.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def coordinates=(value)
self.longitude = value.last
end

# alternative to format_as and update_as
def json_metadata
ActiveSupport::JSON.encode(metadata)
end
# alternative to format_using and update_using
# def json_metadata
# ActiveSupport::JSON.encode(metadata)
# end

def json_metadata=(value)
self.metadata = ActiveSupport::JSON.decode(value)
end
# def json_metadata=(value)
# self.metadata = ActiveSupport::JSON.decode(value)
# end
end
2 changes: 1 addition & 1 deletion spec/system/avo/date_time_fields/date_time_eastern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
subject(:text_input) { find '[data-field-id="posted_at"] [data-controller="date-field"] input[type="text"]' }
before do
CommentResource.with_temporary_items do
field :body, as: :textarea, format_using: ->(value) do
field :body, as: :textarea, format_using: -> do
if view == :show
content_tag(:div, style: "white-space: pre-line") { value }
else
Expand Down
2 changes: 1 addition & 1 deletion spec/system/avo/date_time_fields/date_time_utc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

before do
CommentResource.with_temporary_items do
field :body, as: :textarea, format_using: ->(value) do
field :body, as: :textarea, format_using: -> do
if view == :show
content_tag(:div, style: "white-space: pre-line") { value }
else
Expand Down
2 changes: 1 addition & 1 deletion spec/system/avo/date_time_fields/date_time_western_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

before do
CommentResource.with_temporary_items do
field :body, as: :textarea, format_using: ->(value) do
field :body, as: :textarea, format_using: -> do
if view == :show
content_tag(:div, style: "white-space: pre-line") { value }
else
Expand Down

0 comments on commit f6e2b3f

Please sign in to comment.