Skip to content

Commit

Permalink
refactor: simplify action hooks handling
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeCisco committed Jun 9, 2021
1 parent 3f9dd77 commit 5e80327
Showing 1 changed file with 7 additions and 22 deletions.
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

0 comments on commit 5e80327

Please sign in to comment.