Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify action hooks handling #483

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions lib/forest_liana/bootstrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,6 @@ def display_apimap

private

def get_collection(collection_name)
ForestLiana.apimap.find { |collection| collection.name.to_s == collection_name }
end

def get_action(collection, action_name)
collection.actions.find {|action| action.name == action_name}
end

def generate_action_hooks()
@collections_sent.each do |collection|
collection['actions'].each do |action|
c = get_collection(collection['name'])
a = get_action(c, action['name'])
load = !a.hooks.nil? && a.hooks.key?(:load) && a.hooks[:load].is_a?(Proc)
change = !a.hooks.nil? && a.hooks.key?(:change) && a.hooks[:change].is_a?(Hash) ? a.hooks[:change].keys : []
action['hooks'] = {:load => load, :change => change}
end
end
end

def generate_apimap
create_apimap
require_lib_forest_liana
Expand All @@ -73,15 +53,13 @@ def generate_apimap
if Rails.env.development?
@collections_sent = ForestLiana.apimap.as_json
@meta_sent = ForestLiana.meta
generate_action_hooks
SchemaFileUpdater.new(SCHEMA_FILENAME, @collections_sent, @meta_sent).perform()
else
if File.exists?(SCHEMA_FILENAME)
begin
content = JSON.parse(File.read(SCHEMA_FILENAME))
@collections_sent = content['collections']
@meta_sent = content['meta']
generate_action_hooks
rescue JSON::JSONError
FOREST_LOGGER.error "The content of .forestadmin-schema.json file is not a correct JSON."
FOREST_LOGGER.error "The schema cannot be synchronized with Forest Admin servers."
Expand Down Expand Up @@ -244,6 +222,12 @@ def require_lib_forest_liana
end
end

def generate_action_hooks(action)
load = !action.hooks.nil? && action.hooks.key?(:load) && action.hooks[:load].is_a?(Proc)
change = !action.hooks.nil? && action.hooks.key?(:change) && action.hooks[:change].is_a?(Hash) ? action.hooks[:change].keys : []
action['hooks'] = {:load => load, :change => change}
end

def format_and_validate_smart_actions
ForestLiana.apimap.each do |collection|
collection.actions.each do |action|
Expand All @@ -253,6 +237,7 @@ def format_and_validate_smart_actions
field[:position] = index
end
end
generate_action_hooks(action)
end
end
end
Expand Down